Jump to content

Recommended Posts

Hi All I need some help with a form operation I am working on. You folks might have come across this many a times. Basically I have an array that fills up a drop down menu in a form.

 

$states_array = array("NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "0" => "Other");

 

Now, on form verification i want to repopulate the drop down menu with the users choice selected so I used the standard foreach

 

$state = $_POST['state'];

foreach ($states_arr as $key => $value) {

if (isset($state) && ($state === $key))

echo "<option value=\"$key\" selected>$value</option>";

else

echo "<option value=\"$key\">$value</option>";

}

 

and here lies the problem $state === $key. When i use "===" I get all the states to match, but the "0" => "Other" does not match. If I use "==" then  "0" => "Other" matches but the states don't match and hence won't be selected. I'm assuming this is a type incompatibility. Is there any way around this? Any help would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/73485-solved-need-help-with-form-operation/
Share on other sites

try it like that:

<?php
$state = isset($_POST['state']) ? $_POST['state']:false;
foreach ($states_arr as $key => $value) {
   if ($state == $key)
      echo "<option value=\"$key\" selected>$value</option>";
   else
      echo "<option value=\"$key\">$value</option>";
}
?>

are you sure that the state variable is being set? I dont see any error in your logic..

 

Try this just for testing purposes

 

$state = $_POST['state'];

foreach ($states_arr as $key => $value) {

  if (isset($state) && ($state === $key))

      echo "<option value=\"$key\" selected>$state . ' - '. $value</option>";

  else

      echo "<option value=\"$key\">$state . ' - '. $value</option>";

}

After much debugging of the code, I've discovered that the problem occurs because the index for the "Other" entry in the array is the number zero, not the capital letter "O". Change it to the capital letter and the double equal "==" will work.

 

Here's my code that works:

<?php
$states_array = array("NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "O" => "Other");
?>
<html>
<head>
<title>Select Test</title>
</head>
<body>
<form method="post">
<select name="state">
<?php
$state = (isset($_POST['state']))?$_POST['state']:'';
foreach ($states_array as $key => $value) {
   $sel = ($state == $key && isset($_POST['submit']))?' selected':'';
   echo '<option value="' . $key . '"' . $sel . '>' . $value . "</option>\n";
}
?>
</select><br>
<input type="submit" name="submit" value="Test Select">
</form>
</body>
</html>

 

Ken

 

Lumio, emehrkay,

The $state variable does register and shows on the page when i tested it. but both methods didn't work  :'(.

 

kenrbnsn,

That works great. The letter "O" or word "Other" does work, but why not a number?  ??? Why doesn't php recognize that. There have already been queries designed and entries inputed through this form by other team members. If I change it to letter "O" there will be a lot of grumbling.

Why isn't there a way to compare $state = 0 to $key = 0.

(and congrats on the golden century 5000 posts)

I'm still working on why the numeric index doesn't work correctly. When I was doing my testing, I noticed that both the correct index was matching and the numeric index (i.e. both <option> tags were being marked as "selected").

 

Thanks for the congrats on the 5,000th post. :)

 

Ken

simple fix.. hope

<?php
$states_array = array("NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "0" => "Other");

?>
<html>
<head>
<title>Select Test</title>
</head>
<body>
<form method="post">
<select name="state">
<?php
$state = $_POST['state'];
foreach ($states_array as $key => $value)
{
$key = "$key";
if (isset($_POST['state']) && ($state === $key))
	echo "<option value=\"$key\" selected>$value</option>";
else
	echo "<option value=\"$key\">$value</option>";
}
?>
</select><br>
<input type="submit" name="submit" value="Test Select">
</form>
</body>
</html>

 

Awesome MadTechie!

That just works great.

Hey Ken before you pull your hair out too... this is how i used MadTechie's suggestion

<?php
$state = $_POST['state'];
foreach ($states_array as $key => $value)
{
if (isset($_POST['state']) && ($state === "$key"))
	echo "<option value=\"$key\" selected>$value</option>";
else
	echo "<option value=\"$key\">$value</option>";
}
?>

 

It works great. Thanks!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.