Coming to you with another tut in PHP
Today we are going to be learning about PHP Forms.
We will build the form first...
CODE
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
Enter somthing:
<input name="input" type="text" size="50">
<br />
<div align="right"><input name="submit" type="submit" value="GO!">
</div>
</div>
</form>
<div align="center">
Enter somthing:
<input name="input" type="text" size="50">
<br />
<div align="right"><input name="submit" type="submit" value="GO!">
</div>
</div>
</form>
So lets give that a little explanation.
I'm not going to teach you about the HTML form itself if you want to know more about those. look in the HTML tutorials section of the forums.
There is only 1 bit of code here worth getting into:
CODE
<?php echo $_SERVER['PHP_SELF']; ?>
So!
If you were browsing the page index.php then it would echo:
CODE
<form action="index.php" method="post">
Remember there are limitations to PHP_SELF such as if your browsing index.php?page=downloads
Then it will still only echo index.php rather than ?page=downloads
But i'm sure you can work around that
Ok
So onto our PHP
CODE
<?php
// Start of our script
if($_POST['submit']){
// IF Statement saying that it will only execute the code inbetween the { and the } if they click submit.
$users_input = $_POST['input'];
// Making a variable called users_input with the contents of the text box in it.
// Echo'ing a small line of text including what you wrote.
echo "Many thanks for submitting the form. You typed <b>"$users_input"</b> into the text box.";
// Else - saying that if you haven't clicked the submit button it won't do the PHP.
} else {
?>
// Start of our script
if($_POST['submit']){
// IF Statement saying that it will only execute the code inbetween the { and the } if they click submit.
$users_input = $_POST['input'];
// Making a variable called users_input with the contents of the text box in it.
// Echo'ing a small line of text including what you wrote.
echo "Many thanks for submitting the form. You typed <b>"$users_input"</b> into the text box.";
// Else - saying that if you haven't clicked the submit button it won't do the PHP.
} else {
?>
So i think my comments are generally quite a good explanation but i will explain each line in more detail.
What i like to do is a literal explanation i.e. if they click submit then create a variable
That kind of stuff.
So yeah! Here we go...
"If they click the "submit" button then {
Create variable called users_input with the contents of the form they just filled in then put on the screen:
Many thanks for submitting the form. You typed <b>(What they typed)</b> into the text box.
Otherwise do } { this:"
So yeah...
if($_POST['submit']){ = if the button called submit has been submitted.
$users_input = $_POST['input']; = Make a variable called users_input with their input init.
***NOTE, make sure you understand the correlation between $_POST['input']; & <input name="input" ***
echo "some words"; = Just shows the user what they typed in.
} else { = Otherwise:
Ok so i will now give you the entire PHP page..
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Testing Forms</title>
</head>
<body>
<?php
// Start of our script
if($_POST['submit']){
// IF Statement saying that it will only execute the code inbetween the { and the } if they click submit.
$users_input = $_POST['input'];
// Making a variable called users_input with the contents of the text box in it.
// Echo'ing a small line of text including what you wrote.
echo "Many thanks for submitting the form. You typed <b>"$users_input"</b> into the text box.";
// Else - saying that if you haven't clicked the submit button it won't do the PHP.
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
Enter somthing:
<input name="input" type="text" size="50">
<br />
<div align="right"><input name="submit" type="submit" value="GO!">
</div>
</div>
</form>
<?php
}
// End the script completely
?>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Testing Forms</title>
</head>
<body>
<?php
// Start of our script
if($_POST['submit']){
// IF Statement saying that it will only execute the code inbetween the { and the } if they click submit.
$users_input = $_POST['input'];
// Making a variable called users_input with the contents of the text box in it.
// Echo'ing a small line of text including what you wrote.
echo "Many thanks for submitting the form. You typed <b>"$users_input"</b> into the text box.";
// Else - saying that if you haven't clicked the submit button it won't do the PHP.
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
Enter somthing:
<input name="input" type="text" size="50">
<br />
<div align="right"><input name="submit" type="submit" value="GO!">
</div>
</div>
</form>
<?php
}
// End the script completely
?>
</body>
</html>
Cheers people, hope you have learned somthing
discuss this topic to forum
