Form Submission

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
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)

PHP:
<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>

Okay so now we have the HTML for the document, so below the body tag begin by opening the PHP Tags

PHP:
<?php

Now we need to use an IF statment the check if the submit button has been pressed

PHP:
<?php

if(isset($_POST['submit'])){

$_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)

So now we have called the IF statment, we need to declair a variable

PHP:
<?php
//Check if the submit button has been pressed
if(isset($_POST['submit'])){

//$text is what we put into the field
$text = $_POST['textbox'];

Here we are saying the $text variable should be what we put in the text box on the form

Now we need to tell it what to display

PHP:
<?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;

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 pressed

PHP:
<?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 {

?>

After the submit button code in the form HTML type

PHP:
<?php
}//Ends the else statment
?>

There you have it, your code should now look like this

PHP:
<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>

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top