Handling an HTML form with PHP is one of the most important process in a dynamic Web site. Two steps are involved: first you create the HTML form, and then you create the corresponding PHP script that will receive and process the form data.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<body>
<form action="script.php" method="post">
Name: <input type="text" name="user" /><br />
<input type="radio" name="gen" value="man" />Man
<input type="radio" name="gen" value="woman" />Woman<br />
<input type="submit" name="submit" value="Send" />
</form>
</body>
</html>
- The action attribute dictates to which page the form data will be sent.<?php if (isset($_POST['submit'])) { $user = $_POST['user']; $gen = $_POST['gen']; echo 'User: '. $user. ' - gender: '. $gen; } ?>First, the script use the expresion if (isset($_POST['submit'])) to determine if the form has been submitted (checks if the form element with name="submit" has been received).
If you use method="get" in the form tag, then, in the PHP script you must use $_GET ($_GET['field_name']) or $_REQUEST ($_REQUEST['field_name']).
- With $_REQUEST you can get data from both "post" and "get" methods.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>PHP form example</title> </head> <body> <?php // check if there are received the necessary data from the form if (isset($_POST['user']) && isset($_POST['age'])) { // adds the values in variables, romoving white spaces from the beginning and the end of the values $user = trim($_POST['user']); $age = trim($_POST['age']); // check if data are valid if (strlen($user)>0 && is_numeric($age)) { echo $user. ' - '. $age. ' years old.'; } else { // if not all data are valid echo '<h4>Fill the form with valid data</h4>'; } } else { // if no data from form echo '<h4>Fill the form</h4>'; } ?> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> User: <input type="text" name="user" /><br /> Age: <input type="text" name="age" /><br /> <input type="submit" value="Send" /> </form> </body> </html>- First, the PHP script uses the isset() function to check if the necessary data from the form are sent. Then, if that data are received, adds them in variable, using the trim() function to remove white spaces from the beginning and the end of the values.
The code: <?php echo $_SERVER['SCRIPT_NAME']; ?> outputs the path (the filename) of the current script, often used for the "action" attribute of the form that must send data to the same php file where the form is created.
- You can also have a form submit back to this same page by using no value for the action attribute:
<form action="" method="post">
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net