...

View Full Version : If/Else if seem to both be executing



tarad
07-24-2008, 04:57 PM
Hi there!

All right, here's the situation: we have a form, and a couple of the drop down menus refresh the page (and actually submit the form) when they've been changed. If any of the other drop downs or fields have been changed, the changes disappear (as they should) when the page refreshes.

I'm trying to get one of the drop downs selected item to stick around when the page refreshes.

Here's a chunk of the code:

while ($row = $db->fetch_array($result)) {
echo "<option value=\"$row[0]\" ";
if ($_POST[supporter_id] == $row[0])
{
echo "selected";
}
else if ($info['supporter_id'] == $row[0])
{
echo "selected";
}
echo "> $row[1] </option>";
}

So... It would make sense that the value coming from $_POST is the most recently selected value. So, I'm trying that one first. Then, if its not, then we'll revert to the original one, coming in from the $info array.

A weird thing happens when I view the source after the page has been refreshed/submitted - "selected" is echoed both after the newly selected item from the $_POST variable, and after the original item. I can't figure out why.

Shouldn't the page and form be completely reloading on the refresh, ie. clearing the "selected" off the original item? If something's coming in off the $_POST, then only that part of the if should be executing, right?

Any thoughts would be greatly appreciated. If I'm not making sense, let me know and I'll try to clarify.

Thanks,
Tara

djm0219
07-24-2008, 05:01 PM
Try:



while ($row = $db->fetch_array($result)) {
echo "<option value=\"$row[0]\" ";
if ($_POST['supporter_id'] == $row[0])
{
echo "selected";
}
elseif ($info['supporter_id'] == $row[0])
{
echo "selected";
}
echo "> $row[1] </option>";
}

chaosprime
07-24-2008, 05:30 PM
You're mixing up levels of logic. Your check against $_POST and against $info happens separately for each record, not once for the entire list. So if you have different values for supporter_id in each, both will get the selected attribute. Probably what you need to do is, before this loop runs, clean up / update the value in $info if there was a value in $_POST.

tarad
07-24-2008, 05:43 PM
You're mixing up levels of logic. Your check against $_POST and against $info happens separately for each record, not once for the entire list. So if you have different values for supporter_id in each, both will get the selected attribute. Probably what you need to do is, before this loop runs, clean up / update the value in $info if there was a value in $_POST.

Ahhh yes! Thank you so much. You're so smart. Good idea. This worked perfectly. I appreciate it muchly. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum