Hey Everyone!
Hopefully this will teach some newbies to PHP info on Form Submission!
Okay so first we need to create the HTML for the forms and layout (If you don't know how to do this i suggest you go learn HTML first before you come to learning PHP)
Okay so now we have the HTML for the document, so below the body tag begin by opening the PHP TagsCode:<html>
<head>
<title>Form Submit</title>
</head>
<body>
<form action='index.php' method='post'>
<input type='text' name='textbox'>
<input type='submit' name='submit' value='Submit!'>
</body>
</html>
Now we need to use an IF statment the check if the submit button has been pressedCode:<?php
$_POST is the variable as that's the method we used to submit the data, [' '] is the name of the variable (in this case submit)Code:<?php
if(isset($_POST['submit'])){
So now we have called the IF statment, we need to declair a variable
Here we are saying the $text variable should be what we put in the text box on the formCode:<?php
//Check if the submit button has been pressed
if(isset($_POST['submit'])){
//$text is what we put into the field
$text = $_POST['textbox'];
Now we need to tell it what to display
So up to now, we have told PHP to check if the submit button has been pressed, if it has display what we typed into the box, but we need an else statment now, incase it hasent been pressedCode:<?php
//Check if the submit button has been pressed
if(isset($_POST['submit'])){
//$text is what we put into the field
$text = $_POST['textbox'];
//Display test
echo $text;
After the submit button code in the form HTML typeCode:<?php
//Check if the submit button has been pressed
if(isset($_POST['submit'])){
//$text is what we put into the field
$text = $_POST['textbox'];
//Display test
echo $text;
//If we didnt press submit
}else {
?>
There you have it, your code should now look like thisCode:<?php
}//Ends the else statment
?>
Code:<html>
<head>
<title>Form Submit</title>
</head>
<body>
<?php
//Check if the submit button has been pressed
if(isset($_POST['submit'])){
//$text is what we put into the field
$text = $_POST['textbox'];
//Display test
echo $text;
//If we didnt press submit
}else {
?>
<form action='index.php' method='post'>
<input type='text' name='textbox'>
<input type='submit' name='submit' value='Submit!'>
<?php
}//Ends the else statment
?>
</body>
</html>
Last edited by James.H; 03-08-2010 at 08:10 AM. Reason: Replaced code tags for php tags
Not intending to nitpick, but I think you forgot to mention that the file should be saved as 'index.php'. =b
Otherwise, a fine basic form submission tutorial using PHP, but maybe you'd need to explain what a $_POST variable is; why we should use that and not $_GET, those kind of trivial things. =)
For those who work with various web forms give the special resource - php form tutorials.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks