<?
if ($_POST['submit']){
$ans=0;
echo ("Your answers: ");
echo ("There are ".$_POST['choice1']." days in a week.");
echo ("".$_POST['choice2']." is the first day of the week.");
if($_POST['choice1']=="7"){
$ans=$ans+1;
echo ("Your answer is correct! There are ".$_POST['choice1']." days in a week.");
}else{
echo ("Your answer is incorrect! There are not ".$_POST['choice1']." days in a week.
<?php
if ($_POST['submit']){
$countsub=count($_POST['lista']);
for ($i=0; $i < $countsub; $i++){
echo "<br>".$_POST['lista'][$i];
}
}
else{
?>" method=post>
Qué usted cree es el más importante de vida?
What do you believe is most important in life?
Comida [food]
Agua [water]
Abrigo [shelter]
<?php
if($_POST['submit'] && $_POST['myage'] && $_POST['difference']){
$total= $_POST['myage'] + $_POST['difference'];
echo "My brother/sister is ".$total." years old.";
}else{
?>" >
How old are you?
How many years older is your brother/sister?
JavaScript Form Check
[]{}`\';()@&$#%";
for (var i = 0; i < string.length; i++) {
if (iChars.indexOf(string.charAt(i)) != -1)
return false;
}
for (var i = 0; i < string2.length; i++) {
if (iChars.indexOf(string2.charAt(i)) != -1)
return false;
}
return true;
}
//make check to see if the string only contains numbers, letters
<?php
if ($_POST['submit'] && $_POST['money']){
$dinero = implode($_POST['money'], ",");
$vals=explode(",",$dinero);
?>
How much money would you like to earn per week?
>100
200
<?php
if ($_POST['submit'] && $_POST['food']){
$favfood = $_POST['food'];
?>
<form method="post" action="populate4.php">
What is your favorite food to eat? <BR>
<TEXTAREA NAME="food" ROWS=5 COLS=35 wrap=virtual>
<?php echo $favfood?>
</TEXTAREA>
<input type="submit" name="submit" value="submit">
<form>
<?
}else{
?>
<form method="post" action="populate4.php">
What is your favorite food to eat? <BR>
<TEXTAREA NAME="food" ROWS=5 COLS=35 wrap=virtual></TEXTAREA>
<input type="submit" name="submit" value="submit">
</form>
<?
}
?>
<?php
if ($_POST['submit'] && $_POST['leap']){
$leapy = $_POST['leap'];
?>
A leap year occurs every
<?
if ($leapy!="" && $leapy!="Select number") {
print $leapy;
print ("Select number");
} else {
print ("Select number");}
?>
1
2
3
4
5
6
7
years.
<?
}else{
Populating checkboxes and select lists from an array
When you use checkboxes in a form, usually you want to save more than one choice.
You must indicate to PHP that you are submitting more than one piece of data with []
<INPUT TYPE="checkbox" NAME="temperature[]" VALUE="hot">Hot
<INPUT TYPE="checkbox" NAME="temperature[]" VALUE="cold">ColdAfter submitting your form, save the array data into database.
You can use the implode function joins array elements into a string, then save that string to the
database.
PHP Code
Form Validation
Validating a form
You can perform form validation before a form is submitted via JavaScript or after the form is submitted via PHP.
I've actually already demonstrated simple validation using PHP in Form Processing with PHP: Part 1, with the input.php script. After the form is submitted, the script checks to see whether a variable does or does not exist.
PHP Code:
<?php
//check if submit button clicked and the input field isn't empty, print hello
if ($submit == "click" && $UserName !=""):
echo "Hello, $UserName";
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;
?>
What is your first name?