Forum Moderators: coopster

Message Too Old, No Replies

error correction on drop down list & sessions

         

fintan

9:43 am on Jan 9, 2005 (gmt 0)

10+ Year Member



Hi I'm doing a site at the mo and I have to do error correction on a form. I have most of it done except one or two bits. What I'm having trouble with is drop down lists. What happens is someone fills in the form, it's not done properly so it gets redirected back to the form. Everything works right except the drop down list. The sessions carries over alright but I can't get the drop down list session to work. It marks the session as selected but it's not displayed the first option is displayed. So if you picked Germany and America was first on the list. After being submited America would be displayed but Germany would be checked.

Here is what I got

Correction page

$_SESSION["country"] = $_POST["country"];
if($_SESSION["country"] == '0'){
header("Location: /acc1.php");
}

Form page

echo ' <td><select name="country" id="country" style="width: 150px;">';
if (!isset($_SESSION["courtry"])){
echo '<option value="0" checked="checked"></option>';
}

$rslt = mysql_query($dscntry);
if($rslt){
while ($rws = mysql_fetch_array($rslt)) {
if (!isset($_SESSION["country"])){
echo '<option value="'.$rws["countryid"].'">'.$rws["countries"].'</option>';
}
elseif(isset($_SESSION["country"])){
if($_SESSION["country"] == $rws["countryid"]){
echo '<option value="'.$rws["countryid"].'" checked="checked">'.$rws["countries"].'</option>';
// else{
// echo '<option value="'.$rws["countryid"].'">'.$rws["countries"].'</option>';
// }
}
}
}
}
echo '</select>';

Any ideas? Thanks

fintan.

dreamcatcher

11:03 am on Jan 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you have a typo:

isset($_SESSION["courtry"]))

Should that be:

isset($_SESSION["country"]))

;)

fintan

11:39 am on Jan 9, 2005 (gmt 0)

10+ Year Member



Thanks for spotting that. I've tried it in opera and firefox with no luck. It has to be something I'm not seeing. I'll try cleaning it up a bit.

Here's a cleaner version

echo ' <td><select name="country" id="country" style="width: 150px;">';

if (!isset($_SESSION["courtry"])){
echo '<option value="0" checked="checked"></option>';
$rslt = mysql_query($dscntry);
if($rslt){
while ($rws = mysql_fetch_array($rslt)) {
echo '<option value="'.$rws["countryid"].'">'.$rws["countries"].'</option>';
}
}
}
elseif (isset($_SESSION["courtry"])){
echo '<option value="0"></option>';
$rslt = mysql_query($dscntry);
if($rslt){
while ($rws = mysql_fetch_array($rslt)) {
if ($_SESSION["courtry"] == $rws["countryid"]){
echo '<option value="'.$rws["countryid"].'" checked="checked">'.$rws["countries"].'</option>';
}
else{
echo '<option value="'.$rws["countryid"].'">'.$rws["countries"].'</option>';
}
}
}
}
echo '</select></td>';

fintan

12:07 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



Sorry for posting just copped what I did wrong. It's always the most stupid mistakes. Was staring a the code to long I guess. Change checked to selected. Doh!