Jump to content

Using "value = <?print $listbox; ?>" on forms?


Solarpitch

Recommended Posts

Hey Guys,

 

You know when you submit a form on the same page and you display a list of errors back to the user . . please fill username etc.

 

You then use value="<?print $firstname; ?>" and so on to fill in the fields that the user has already entered correctly so they dont have to fill that part in again.

 

Just want to know how you do it for list boxes because when you use value="<?print $category; ?>" for a listbox it doesnt seem to work.

 

Link to comment
https://forums.phpfreaks.com/topic/53853-using-value-on-forms/
Share on other sites

<select name="listbox" size=20>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

 

It is because a list box is setup differently. You would have to format the $category and have each option laid out like above.

Link to comment
https://forums.phpfreaks.com/topic/53853-using-value-on-forms/#findComment-266299
Share on other sites

Here's how I do it:

 

<select name="state">

<?php

$user_state = $_POST['state'];

$state_list = array(
  "AL" => "Alabama ",
  "AK" => "Alaska ",
  "AZ" => "Arizona ",
  "AR" => "Arkansas ",
  "CA" => "California ",
  "CO" => "Colorado ",
  ... );

foreach ($state_list as $key => $value) {
  $selected = "";
  if ($key == $user_state)
  $selected = 'selected="selected"';
  // Loop HTML
  ?>
  <option <?=$selected?>value="<?=$key?>"><?=$value?>   </option>
  <?php		
  // End HTML		
}

?>
</select>

 

Keep in mind that I have short-tags enabled.

Link to comment
https://forums.phpfreaks.com/topic/53853-using-value-on-forms/#findComment-266310
Share on other sites

Hey thanks guys,

 

I figured out another way to do it...

 

 

So if you submitted a form check this by setting process == 1 and fill the options based on it..

 

<? if ($_POST['process'] == 1)

{

print $county;

}

else

{

print "Please Select...";

 

}

?>

Link to comment
https://forums.phpfreaks.com/topic/53853-using-value-on-forms/#findComment-266316
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.