refill form options

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

refill form options

Post by kristolklp »

I have an order form setup with input fields, radio buttons, checkboxes, and comboboxes. My form also contains a 'submit order' button.

When the user clicks 'submit order' my form posts all variables back to the php file and checks for errors (incomplete fields, etc.). If there is an error I want the user selections to stay on the form so they do not have to enter ithe same nformation again. I know how to fill the text fields in with the posted variables, but how do I handle checkboxes, radio buttons, combo boxes, etc.?

Code: Select all

<input type="text" name="name" value="<?php echo $name ?>"  size="20">
Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

checked="checked" (checkboxes and radio buttons)
selected="selected" (combo boxes)

you'll have to compare the value they chose against each value of each group
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

just make your form like this

Code: Select all

<input type="text" name="first" value="<?= $_POST['first'] ?>">
<select name="second">
  <option value="somting" <?= ($_POST['second'] == 'somting' ? 'selected' : '') ?>>
</select>
and you should be good if you are not using error_reporting(E_ALL)

edit: i was a bit slow i knew i would be :oops:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

<input type="text" name="name" value="<?php echo $name ?>"  size="20">
Make sure to use $_POST (or $_GET if you are using a get form)

Using just the variable name is not good.
kristolklp
Forum Commoner
Posts: 30
Joined: Mon Sep 05, 2005 2:24 pm

Post by kristolklp »

Thanks for the help!
Make sure to use $_POST (or $_GET if you are using a get form)

Using just the variable name is not good.
I actually set the variable during my error trapping before it gets back doen to the form.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

Just to add to this (although yiou are setting you vars in the script).

I find that using the $_REQUEST variable works better in situations where you want to pre-submit the form, i.e. a post action to comit, but use a get action to preview before comitting

Just my two pennies worth :D
Post Reply