Form Processing with PHP: Part 3
How to populate form fields with previously submitted values.
When you see the scripts in action, take a look at the source code, especially what the values of the form fields are set to before and after the scripts execute.
Input field:
To automatically have the value of an input field filled out, use the echo/print function to place the value submitted into the value attribute of the field.
PHP Code:
<?php
if ($submit && $firstname){
$fname = $firstname;
?><form method="post" action="populate1.php3">
What is your first name?
//Check to see if a value was submited then write it to the value attribute of the input field
<INPUT TYPE="text" NAME="firstname" value="" max_length=15 size=20> <input type="submit" name="submit" value="submit">
</form>
<?
}else{
?>
....
<?
}
?>See it in action. ¦ Get complete code.
Select List Boxes:
To automatically have a select list display the option value that you selected when you submitted the form, check to see if the variable is not empty and isn't the default value. If there's a value for the variable, use the echo/print function to output an option with the previously submitted value.
<?php
if ($leapy!="" && $leapy!="Select number") {
print $leapy;print ("
Select number");} else { print ("Select number");}
?>PHP Code:
<?php
if ($submit && $leap){
$leapy = $leap;
?><form method="post" action="populate2.php3">
A leap year occurs every
<SELECT NAME="leap">
<OPTION>
//Check to see if, the value submited is not - select number -
//then set the value of the option to contains the preselected value
<? if ($leapy!="" && $leapy!="Select number") {
print $leapy;print ("
Select number");} else { print ("Select number");}?>
<option value="1">1
<option value="2">2
..... years.
<input type="submit" name="submit" value="submit">
</form>
<?
}else{
?>
....
<?
}
?>See it in action. ¦ Get complete code
Check Boxes:
Place checkbox values into array using implode function" $cols = implode($colors, ",");
When you want to see what check boxes were checked, you use the explode function to get the values from the array: $colarray=explode(",",$cols);
In the case where you are placing values into a database, you would insert the value of $cols into a table column, then retrieve the value of the table column and assign it to a value, i.e. $tablecolumn, then use the explode function - $cols=explode(",",$tablecolumn);
PHP Code:
<?php
if ($submit && $colors){
//put the values from the boxset into an array
$cols = implode($colors, ",");
//get the values of the boxset from an array
$colarray=explode(",",$cols);
?><form method="post" action="populate3.php">
What color(s) do you like?
//Check to see if a boxset value is the array, if it is, make the checkbox active
<INPUT TYPE="checkbox" NAME="colors[]" VALUE="Yellow"
<? for ($index=0; $index< count($colarray); $index++){
if ($colarray[$index]=="Yellow") { print(" CHECKED");}} ?> >Yellow
....
<input type="submit" name="submit" value="submit">
</form>
<?
}else{
?>
....
<?
}
?>See it in action. ¦ Get complete code
Radio Buttons:
Are basically the same as checkboxes, but only one value can be checked if you give the same name to set of radio buttons. You must give the buttons different names if you want to select more than one.
PHP Code:
<?php
if ($submit && $money){
//put the values from the radioset into an array
$dinero = implode($money, ",");
//get the values of the radioset from an array
$vals=explode(",",$dinero);
?><form method="post" action="populate5.php3">
How much money would you like to earn per week?
//Check to see if a radioset value is the array, if it is, make the radio button active
<INPUT TYPE="radio" NAME="money[]" VALUE="100"
<? for ($index=0; $index< count($vals); $index++){
if ($vals[$index]=="100") { print(" CHECKED");}} ?> >100
....
<input type="submit" name="submit" value="submit">
</form>
<?
}else{
?>
....
<?
}
?>See it in action. ¦ Get complete code
Textareas:
make sure there is no whitespace in your file because when echoing back your info in the textare it may not appear correctly.
PHP Code:
<?php
if ($submit && $food){
$favfood = $food;
?><form method="post" action="populate4.php3">
What is your favorite food to eat?
//Check to see if a value was submited then write it between the textarea tags
<TEXTAREA NAME="food" ROWS=5 COLS=35 wrap=virtual>
<?php echo $favfood?>
</TEXTAREA>
<input type="submit" name="submit" value="submit">
<form>
}else{
?>
....
<input type="submit" name="submit" value="submit">
</form>
<?
}
?>