Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Form Submission


  • Please log in to reply
3 replies to this topic

#1 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 01 March 2010 - 02:40 PM

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)

<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


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

<?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
//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
//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
//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
}//Ends the else statment
?>


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

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

Edited by James.H, 08 March 2010 - 08:10 AM.
Replaced code tags for php tags

  • 0

#2 k1net1cs

k1net1cs

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 04 March 2010 - 08:35 AM

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. =)
  • 0

#3 kailas

kailas

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 08 March 2010 - 08:05 AM

For those who work with various web forms give the special resource - php form tutorials.
  • 0

#4 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 08 March 2010 - 08:11 AM

Good tutorial, thanks + rep!
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download