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

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.

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.
 

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
Within a select element, select the predetermined option. I can see how, although, assume I have a list as such:
HTML:
<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:
<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>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:
<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:
$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:
$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.
 
Top