Copy link to clipboard
Copied
Hi I have a drop down list being populated by a mysql table, the dropdown list works fine with read and update but on the edit page I would like to have the mysql stored id to be selected in the drop down list.
I have the following code but I can not get the item to be selected? I think it is because I have not coded the if statement correctly,
<select name="stand_id">
<?php
$stand_set = getall_stands();
while ($stands = mysql_fetch_array ($stand_set) ) { ?>
<?php if (isset($_POST['stand_id']) == $url_show ['stand_id']) echo 'selected="selected
"'; ?>
<option value=" <?php echo $stands ['stand_id']; ?>"
><?php echo $stands ['stand_descrip']; ?></option>
<?php } ?> </select>
Any help would be really appreciated
Horsemad Gilly wrote:
the argument would be $stands ['stand_id'] being compared with $url_show ['stand_id'] and then selecting the $stands ['stand_id']
<?php if ((isset($_POST['stand_id'])) && ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>
Copy link to clipboard
Copied
Without the benefit of seeing all the code it is a little tough to see if the logic is valid, but something like this should work:
<?php
$stand_set = getall_stands();
while ($stands = mysql_fetch_array ($stand_set) ) {
if (isset($_POST['stand_id']) == $url_show ['stand_id']);
?>
<select name="stand_id">
<option><?php echo [add your code]; ?></option>
<option><?php echo [add your code]; ?></option>
<option><?php echo [add your code]; ?></option>
</select>
<?php }?>
Copy link to clipboard
Copied
gary,
What is the purpose of your modified code? I'm not seeing selected in your code, which is what the OP clearly wants to achieve. Your code would repeat the entire select menu, not the options. Additionally, you are aware that isset returns true or false, so the if statement will not do anything in your code.
Gilly, try this:
<select name="stand_id">
<?php $stand_set = getall_stands();
while ($stands = mysql_fetch_array ($stand_set) ) { ?>
<option value="<?php echo $stands['stand_id']; ?>"<?php if ((isset($_POST['stand_id'])) && ($_POST['stand_id']) == $url_show['stand_id'])) { ?> selected="selected"<?php } ?>><?php echo $stands['stand_descrip']; ?></option>
<?php } ?>
</select>
best,
Shocker ![]()
Copy link to clipboard
Copied
Hi thanks for the feed back, I think the issue is with the if statement.
The value $url_show ['stand_id'] is the individual value stored in the table. The drop down list value is also coming from the database and if the form has not been submitted it will not be showing a $_POST. So I need it to be comparing against the ID coming from the list containing the drop down items, this is being pulled from the database with
$stand_set = getall_stands();
while ($stands = mysql_fetch_array($stand_set))
so the argument would be $stands ['stand_id'] being compared with $url_show ['stand_id'] and then selecting the $stands ['stand_id']
Copy link to clipboard
Copied
Horsemad Gilly wrote:
the argument would be $stands ['stand_id'] being compared with $url_show ['stand_id'] and then selecting the $stands ['stand_id']
<?php if ((isset($_POST['stand_id'])) && ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>
Copy link to clipboard
Copied
OOOOOOOHHHHH THANK YOU THANK YOU THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! A Major break through, this was stopping me from completing a whole load of pages, I owe you a drink!!!!!!!
Copy link to clipboard
Copied
There were a couple of changes so the final code that works is
<?php if ((isset($_POST['stand_id']) || ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>
Fabulous really appreciated, but I think you might have guessed that by now.
Copy link to clipboard
Copied
You code:
<?php if ((isset($_POST['stand_id']) || ($stands ['stand_id']) == $url_show ['stand_id'])) { ?> selected="selected"<?php } ?>
Basically says if the form is submitted (isset = true) OR if something equals something then display the selected for the option. Upon looking at my code I discovered a syntax error, but the code logic basically said if the form was submitted (isset = true) AND if something equals something. If you're using the OR argument you should be able to dump the condition of whether the form was submitted and simply check if something equals something like this:
<?php if ($stands ['stand_id'] == $url_show ['stand_id']) { ?> selected="selected"<?php } ?>
Thanks for the drink offer... but I'm already drunk!
best,
Shocker ![]()
Find more inspiration, events, and resources on the new Adobe Community
Explore Now