Results 1 to 5 of 5

Thread: How to set a predetermined value into an HTML select element via PHP

  1. #1
    sikuneh is offline x10Hosting Member
    Join Date
    Dec 2008
    Posts
    55

    How to set a predetermined value into an HTML select element via PHP

    I have a review site and just added a preview button. I have a select element with a score rating from 1 to 100, and I was wondering how to set the value as the value that was selected when the preview button was pressed. Basically, how do I set the value from the previous page in a select statement? The value will be provided using the PHP $_POST['score'] method.
    Last edited by sikuneh; 03-25-2012 at 05:33 AM.

  2. #2
    misson is offline x10 Spammer
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,573

    Re: How to set a predetermined value into an HTML select element via PHP

    To select an option in a select element by default, add the "selected" attribute to the option element, as per the various HTML specs. That's as precise as I can get using the information you've provided. When asking for help with code, include a minimal test case. Read my sig for more.

    Quote Originally Posted by sikuneh View Post
    Basically, how do I set the value from the previous page in a select statement?
    Do you mean select element? A select statement is something else entirely.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Jon Skeet's and Eric Raymond's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    sikuneh is offline x10Hosting Member
    Join Date
    Dec 2008
    Posts
    55

    Re: How to set a predetermined value into an HTML select element via PHP

    Within a select element, select the predetermined option. I can see how, although, assume I have a list as such:
    HTML Code:
    <select name="list">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    If it was susceptible to change, based on the review score. How would I set the option that is referenced by the score gotten from the database.

    Like if the score was 2, how would I set that in the above select element with PHP?

    Maybe something like
    PHP Code:
    <select name="list">
    <?php
    $list_length 
    3;
    // Right here would be a connection to the database to retrieve
    // the stored value which would truly set $stored_score
    $stored_score 2;
    for (
    $i 0$i $list_length$i++) {
        if (
    $i == $stored_score) {
            echo 
    "<option value='{$i}' selected>{$i}</option>";
        }
        else {
            echo 
    "<option value='{$i}'>{$i}</option>";
        }
    }
    ?>
    </select>

  4. #4
    adam.k is offline x10Hosting Member
    Join Date
    Sep 2008
    Posts
    41

    Re: How to set a predetermined value into an HTML select element via PHP

    That should work. I've used code like that before.

  5. #5
    misson is offline x10 Spammer
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,573

    Re: How to set a predetermined value into an HTML select element via PHP

    As Adam points out, it should work, though your starting index is wrong if you want to start at 1. Did you try it?

    However, it duplicates code by printing an <output> element in more than one place. You can avoid this by setting a variable to hold optional attributes.

    PHP Code:
    <select>
      <?php for ($i=1$i $list_length; ++$i) {
        if (
    $i === $selected) {
          
    $attrs 'selected';
        } else {
          
    $attrs '';
        }
        
    ?>
        <option <?php echo $attrs ?>><?php echo $i ?></option>
      <?php ?>
    </select>
    (Note: if the value of an option element is to be the same as its content, you can leave off the value attribute. The value for an option element defaults to its text content.)

    To avoid having to check for the selected option each time through the loop, assemble the document first, then set the selected option outside the loop. For example, you can do this with SimpleXML:
    PHP Code:
    $doc = new SimpleXMLElement('<html />');
    $select $doc->addChild('select');
    for (
    $i 1$i $list_length; ++$i) {
        
    $option $select->addChild('option'$i);
    }
    $select->option[$selected-1]->addAttribute('selected','selected');

    echo 
    $select->asXML(); 
    or with DOM:
    PHP Code:
    $doc = new DOMDocument();
    $select $doc->createElement('select');
    for (
    $i 1$i $list_length; ++$i) {
        
    $option $doc->createElement('option'$i);
        
    $select->appendChild($option);
    }
    $select->childNodes->item($selected-1)->setAttribute('selected'TRUE);
    $doc->appendChild($select);

    echo 
    $doc->saveHTML(); 
    There isn't much of a gain for any approach, so use whatever you wish. One potential advantage of the XML parsers (SimpleXML and DOM) is that you can define the structure for data in one place (DRY, again) and process it for whatever purposes you need, such as creating forms or displaying data. It becomes very useful in an MVC architecture, as you can build up the document (or parts of the document), with different modules making changes as necessary until processing has finished.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Jon Skeet's and Eric Raymond's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

Similar Threads

  1. PHP: Problem with For Loops and If Statement
    By PharaohInc in forum Scripts, 3rd Party Apps, and Programming
    Replies: 4
    Last Post: 05-16-2010, 05:17 PM
  2. How to Auto select User's country in registration form's select menu using PHP
    By oceanwap in forum Scripts, 3rd Party Apps, and Programming
    Replies: 5
    Last Post: 04-15-2010, 05:25 AM
  3. PHP MYSQL WHERE statement help
    By Chris S in forum Scripts, 3rd Party Apps, and Programming
    Replies: 3
    Last Post: 10-23-2008, 02:13 PM
  4. MySQL and if statement...
    By chappill in forum Scripts, 3rd Party Apps, and Programming
    Replies: 1
    Last Post: 08-28-2008, 01:18 PM
  5. if statement question
    By votter in forum Scripts, 3rd Party Apps, and Programming
    Replies: 4
    Last Post: 08-25-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
dedicated servers