loader

PHP Forms

by on April 18, 2011 | Posted in Scripts and Code

Due to the popularity of this post on my programming blog, I copied it over here as well.  We don't really talk much here about scripts and code, but I thought this would be useful information for anyone looking to make a PHP form. There are a million tutorials on this and though I am not one to beat a dead horse, I am going to anyway.  Here is my quick breakdown on PHP forms with a super simple tutorial.

The Form

So to create a form we need to define the form area. we do so using HTML tags

<form> </form>

All the form data inside these tags will be send when the submit button is pressed. The Action Your form needs to have an action, an action determines where the information goes when the submit button is pressed. The action is placed inside the form tag. The action is a page or a url. Lets say you want to send the form data to a page called process.php. If process.php is in the same directory as your form page then you can simply put action="process.php". In this example we are going to make the page submit to itself. Now because I test my script on one site then transfer to another site, i don't want to have to change my script every time I move it. Using the global variable $_SERVER we can get the information we need dynamically. In s script we need to get the exact path to our form page, regardless of where it is on the server. And after submitting the form isn't going to show, so we are going to user the SCRIPT_NAME variable. This is also a php function and must be placed in php tags. So to put this into action our first line of the form looks like this:

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>"   >

The Method

Your form also needs a method. The method tells your form how to send it's variable. We are going to work with the variables POST and GET. The POST variables are sent unseen to the action URL. To retrieve post variable you use $_POST['variablename']. if you send variables POST to yourdomain.com you would see http://www.yourdomain.com in the address bar, and your accepting page would process the variables unseen. Now lets say we sent a variable GET. Our form sent a variable of age that was 42 and name that was Frank to http://www.yourdomain.com. In the address bar you would see http://www.yourdomain.com?age=42&name=Frank. To retrieve these values you would use $_GET['age'], thus using echo $_GET['age']; in your php tags would show 42 on your page.

The Data

So how does data get sent? The data come from input fields, we're not going to cover textareas, combo boxes, or checkboxes in this tutorial. We start with the basic input.

<input   />

First, we are going to welcome our user to our domain after they send the form. Even though we could get this data on the submitted page, we're going to get it now. To send this data easily with the form, we can simply make a hidden field.

<input type="hidden"

Easy enough, now we need to give the input a name, this is the variable name that will be sent.

<input type="hidden" name="site_url"

Don't use spaces in variable names, it tends to anger the internet god. now we need to give it a value. The value set will be the value of the variable name when the form is submitted. Let say this script is going to possibly have it's name changed. We go back to the $_SERVER variable, this time we will use PHP_SELF. Don't forget the php tags.

<input type="hidden" name="site_url" value="<?php echo $_SERVER['PHP_SELF']; ?>" />

  Now we are going to make an input box for the user to type their name. The input type is no longer hidden, but is now text. Our input would look like it does below.

<input type="text" name="name" value="" />

You can set the input box length and max characters, as we are already doing the beat a dead horse thing, you can get more attributes here. Our final input is the submit. By creating a submit button you allow users to send the data entered. The button creates itself, again attributes, CSS, etc. you can style the button to your liking. For simplicity's sake, a submit button. Notice our type is now submit. The value now becomes the test we see on button.

<input type="submit"  name="submit" value="Send" />

Seeing the Variables So we just hit submit, now what? The form sends the data to itself, collects the information and process it accordingly. How does the page know that the form has been submitted and its time to process the form? There are many ways to look to see if the form was submitted, we are simply going to check to see if the submit button was hit by using isset($_POST['submit']). isset checks the POST variable to see if it has a value, combine with an if statement. Therefor:

if (isset($_POST['submit'])){
//form was sent
}else{
//form was not submitted
}

The Assembly

So there's a briefing of the basics. Now lets put it together. Before we make our form we need to decide on how our page will flow since this will be a single page. This example will use both a GET and POST method. We will submit the form with a POST and then use GET with a return link. We are going to return back to the form after we submit and check for a name, so we will check for that variable first. We are going to send the return variable via GET. The first part of our form will look like this:

<?php
if(isset($_GET['name'])){
echo 'You have already entered your name '.$_GET['name'].' to the page ' .$_POST['site_url'].'. You can enter a new name below if you wish to change it.<p><p>';
}

Then we add the variable to check for the form submission.

if (isset($_POST['submit'])){
echo 'Thank you for submitting the form '. $_POST['name'].' to '.$_POST['page_url'] .'!<br />';

  Then we make our return link back tot he page again to allow for a name change. Notice how we use the variable name in the url to send it via get.

echo  '<a href ="'.$_SERVER['REQUEST_URI'].'?name='.$_POST['name'].'"> Click here to return</a>';
}else{
?>

Now we build the actual form.

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<input type="hidden" name="page_url" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Send" />
</form>

Close our php if statement.

<?php } ?>

The copy and paste

For you copy and pasters, together we now have this:

<?php
if(isset($_GET['name']) && !$_POST['submit']){
echo 'You have already entered your name '.$_GET['name']. '. You can enter a new name below if you wish to change it.<p><p>';
}
if (isset($_POST['submit'])){
echo 'Thank you for submitting the form '. $_POST['name'].' to '.$_POST['page_url'] .'!<br />';
echo  '<a href ="'.$_SERVER['REQUEST_URI'].'?name='.$_POST['name'].'"> Click here to return</a>';
}else{
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<input type="hidden" name="page_url" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Send" />
</form>
<?php } ?>

There is a simple form where variables are sent and received. I hope i broke this down enough, later I hope to add on with combox code, email forms, and more. As always please leave your feedback and suggestions.

No Comment
Read More