Jump to content

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


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

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.