Jump to content

Debug Code for retaining selection


killdozer

Recommended Posts

I'm stuck trying to figure out why when I make a selection in drop down list of a php form, the selection does not stay when form is refreshed or re-displayed due to an pattern error of another field.

 

My array is

state_province = array(list of the states, and provinces)

field name in the label array is: "state" => "State",

Here is the code that I'm using:

 

echo "<form action='$_SERVER[php_SELF]' method='POST'>";

foreach($labels as $field => $label)

{

if($field == "state")

{

echo "<div class='province_state'><label for='state' size='15'>* Province/State</label><select>";

 

foreach($state_province as $state)

 

{

echo "<option value=$state";

if(@$_POST['state'] == $state)

  {

echo "selected='selected'";

  }

echo ">";

echo $state;

echo "</option>\n";

}

  echo "</select></div>\n";

 

}

 

 

Link to comment
Share on other sites

Ok I changed it to this, but no change for the retention:

 

echo "<form action='$_SERVER[php_SELF]' method='POST'>";

foreach($labels as $field => $label)

{

if($field == "state")

{

echo "<div class='province_state'><label for='state' size='15'>* Province/State</label><select name='state' id='state'>";

 

foreach($state_province as $state)

 

{

echo "<option value= $state";

if(@$_POST['state'] == $state)

  {

echo "selected='selected'";

  }

echo ">";

echo $state;

echo "</option>\n";

}

  echo "</select></div>\n";

 

}

 

 

 

 

 

 

 

 

Your <select> doesn't have a name. Unless you make the <label> end after </select>, you'll need to set its name.

Link to comment
Share on other sites

Sorry, I guess I forgot to mention, you have a space between the value attribute and $state, so to the browser that means it has no value and that you have an invalid attribute called $state. It should also be in quotes.

echo "<option value='$state'";

Link to comment
Share on other sites

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.