0

I am using php and trying to change a option using php my code looks like this:

<?php
$ProductName = 'test2';
?>

<select id="product" name="product" value=<? echo $ProductName; ?> >
   <option value="test1">Option 1</option>
   <option value="test2">Option 2</option>
   <option value="test3">Option 3</option>
   <option value="test4">Option 4</option>
</select>

however the above does not change the option.

any Ideas?

4 Answers 4

2

There is a couple of ways to do this. First off the HTML you are looking for is:

<option value="test2" selected="selected">Option 2</option>

I prefer the Javascript/jQuery method myself as it doesn't clutter the hell out of my code. After the select HTML:

$("#product option[value='<?php echo $productName;?>']").attr('selected','selected');
Sign up to request clarification or add additional context in comments.

Comments

1

you have to something like this:

<select id="product" name="product" value="" >
    <option value="test1"<? if($ProductName == "test1") echo " selected"; ?>>Option 1</option>
    <option value="test2"<? if($ProductName == "test2") echo " selected"; ?>>Option 2</option>
    <option value="test3"<? if($ProductName == "test3") echo " selected"; ?>>Option 3</option>
    <option value="test4"<? if($ProductName == "test4") echo " selected"; ?>>Option 4</option>
</select>

but that would be good to have the values and names of options in an array and do this check in a loop

1 Comment

but I would suggest to use Tim's solution, it is very elegant and nice solution IMO
1

You need to add the selected attribute to the <option> element. You might consider something like this:

<?php
$options = array('test1' => 'Option 1',
                 'test2' => 'Option 2', ...);

$selected = 'test1';
?>

<select id="product" name="product">
   <?php foreach ($options as $value => $text): ?>
      <option value="<?=$value?>"<?=$value == $selected ? ' selected' : ''?>>
        <?=$text?>
      </option>
   <?php endforeach; ?>
</select>

1 Comment

@edwardmp are you kidding? They were posted 2 minutes apart and it's a fairly 'standard' way of doing it. Besides, mine wont throw a notice when the $_GET array is empty.
0

When using a selectbox, the correct HTML to do this would be something like this:

<select id="product" name="product">
   <option value="test1" selected>Option 1</option>
   <option value="test2">Option 2</option>
   <option value="test3">Option 3</option>
   <option value="test4">Option 4</option>
</select>

So add the "selected" word at the end of the value you want to select.

In PHP you could do it like this easily:

<select id="product" name="product">
<?php
$options = array(1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3');
foreach ($options as $key => $value) { 
   echo '<option value="' . $key . '"' . ($key == $_GET["test"] ? ' selected="selected"' : '') . '>' . $value . '</option>';
} ?>
</select>

2 Comments

but I want to be able to change the value based on the php value?
then you need to modify that code to show up in the part where it says "Option 1". That is the 'value' that is displayed to the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.