Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Accessing Form Data
This tutorial will teach you how to access form data using PHP's _POST and _GET globals.

In PHP there is a large amount of variables that are predefined and ready to be utilized in your scripts if needed. A couple examples of these predefined variables are the _POST and _GET arrays. These arrays are used a lot when handling data submitted through forms.

If you have some experience with HTML forms, you know that there are two methods of submitting the data collected by forms. There is the POST method, which silently submits the data to the server, and there is the GET method, which submits the data through the URL and is visible in the address bar of your browser.

The global variable used to access the data depends on which method you use in the form. Let's look at an example, suppose you have the form below and you are using the POST method to submit it.

<form action="process.php" method="POST">
  Name: <input type="text" name="name" /><br />
  Email: <input type="text" name="email" /><br />
  <input type="submit" value="Submit" />
</form>

Once the user clicks submit, all of the data from the form inputs will be available to the script specified in the action attribute. (in this case process.php)

To access this data, all you need to do is use the global variable (POST or GET) like you would a normal associative array. Here is a simple example of what could be used for process.php.

<?php

  
echo 'Hello '.$_POST['name'].', I will email
you at '
.$_POST['email'].' as soon as possible.';

?>

Notice that we use the name of the input tag to access it in the _POST/_GET array, so if you had an input named 'message', you'd access it like this: $_POST['message'] or $_GET['message']... depeding on which action you chose.

Hopefully this tutorial gave you a better idea how to access form data. If you have any questions, please use the discuss link below.

Discuss Tutorial: Accessing Form Data 7 Comments
Comment by Ron on Apr 13, 2004, 12:00 am
Please post questions or comments about this tutorial below. Smile
Comment by DarkJedi613 on May 20, 2004, 9:29 pm
You do not need to use the $_POST["var_name"]/$_GET["var_name"] array to access variables you can also access them through $var_name in either case. Though it is good to know the $_POST/$_GET arrays - you can generate variables on the fly with them.

Also if you're having trouble with $_POST/$_GET arrays try using $HTTP_POST_VARS/$HTTP_GET_VARS (the older versions used that array).
Comment by Ron on May 22, 2004, 8:29 am
Actually, you may only use the short hand($var_name, as you put it) if register_globals is on in the php.ini file, the default is off, as of PHP 4.2. So it's best to use $_POST['var_name']/$_GET['var_name']
Comment by Eric on Jun 4, 2004, 1:32 am
What would be the process.php part? How would I get it to send to someone?
Comment by Paulenas on Jun 9, 2005, 6:58 pm
How can i do form to store e-mails to TXT files with PHP?
Any suggestions?
Comment by matt on Jun 18, 2005, 1:55 pm
the process.php part is

<?php
echo 'Hello '.$_POST['name'].', I will email
you at '.$_POST['email'].' as soon as possible.';
?>

as it said above. to get the data inputed into the form to mail to someone, ad the following code:
mail("nobody@example.com", "My Subject", $_POST['var_name']);
correct me if I'm wrong, but I beleive that's the way to do it.
Comment by Forgotten on Jun 23, 2005, 8:23 pm
How do I handle Radios?

Please email me the answer!! Wassat

« Previous [ 1 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.