Professional Documents
Culture Documents
Contents
Working more on html form.
Validating form data.
HTML forms that allow the user to pick more than one
selection in a list or a check box are created with select tags
(when the "multiple" attribute is set) and checkbox input
types.
Instead of PHP generating a single variable for multiple
choices, an array is better suited for that task.
Example
<select name="movies[]">
<input type=“checkbox” name="colors[]" value="red">
(The HTML form)
<html><head><title>Multiple Choice</title></head>
<body bgcolor="aqua">
<form action="checkbox.php" method="POST">
<b>Choose a vacation spot:</b>
<br />
<select name="location[]" multiple="multiple">
<option>Maui
<option>Bali
<option>Miami
<option>Riviera
</select> <p>
<b>Select a city:</b>
<br />
<input type="checkbox" name="place[]" value="New York"
/>NewYork
<br />
<input type="checkbox" name="place[]" value="Chicago" />Chicago
<br />
<input type="checkbox" name="place[]" value="London" />London
<br />
<input type="checkbox" name="place[]" value="Tokyo" />Tokyo
<br />
<input type="checkbox" name="place[]" value="San Francisco"
Checked />San Francisco
<p>
<input type="submit" value="submit" />
<input type="reset" value="clear" />
</form>
</body>
CodeView:
<html><head><title><Forms and User-defined Arrays>
</title></head>
<body bgcolor="8CCCCA">
<br />
<fieldset><legend><b>Vacation Choices</b></legend>
<?php
// Medium style
if (is_array($_POST['location']))
{
print "<ul>";
foreach ( $_POST['location'] as $key=>$value )
{
print "<li>$key=>$value</li>";
}
print "</ul>";
}
?>
</fieldset>
<fieldset><legend><b>City Choices</b></legend>
<?php
//extract($_POST);
if (is_array($_POST[„place‟]))
{
print "<ul>";
foreach ( $place as $key=>$value )
{
print "<li>$key=>$value</li>";
}
print "</ul>";
}
?>
</fieldset></body></html>
SELF-PROCESSING HTML FORMS
Rather than creating a separate HTML document to
display a form and another PHP script to process the user
input, you might want to combine the HTML document
containing the form and the PHP script that processess it
all into one script.
This is done by assigning the $_SERVER['PHP_SELF']
array to the action attribute of the HTML <form> tag
Examples
<?php
// Was the form submitted?
$your_name=$_POST['your_name'];
$your_phone=$_POST['your_phone'];
print "<b>Your name is $your_name<br />";
print "Your phone is $your_phone<br />";
?>
Code View:
<?php
if ( isset($_POST['submit']))
{
// Was the form submitted?
$your_name=$_POST[„your_name‟];
$your_phone=$_POST[„your_phone‟];
print "<b>Your name is $your_name<br />";
print "Your phone is $your_phone<br />";
print "The path to this file is: “.$_SERVER['PHP_SELF']."<br />";
}
else{ ?>
<html><head><title>HTML Form</title></head>
<body bgcolor="lightblue"><font size="+1">
<form action="<?php echo $_SERVER['PHP_SELF'];?>”
method="POST">
}
<p />
Please enter your name: <br />
<input type="text" size=50 name="your_name">
<p />
Please enter your phone: <br />
<input type="text" size=50 name="your_phone">
<p />
<input type="submit" name="submit" value="Send Now">
<input type=reset value="Clear">
</form>
<hr>
</html>
<?php } ?>
END!!!