Jump to content

  •  

Photo
- - - - -

P10: Passing Variables P1 POST

PHP4Beginners

  • Please log in to reply
No replies to this topic

#1 DraxxTV

DraxxTV

    Member

  • SuperGeek
  • PipPip
  • 14 posts

Posted 06 July 2014 - 09:45 PM

POST

The post method allows a user to collect information from a form when sent with method="post". When information is sent this way, it is done invisibly using HTTP headers. There is no restrictions when it comes to the amount of data that can be sent however, there is an 8 MB max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file). Information sent can be accessed by viewing the contents inside the $_POST array.

 

Creating a Form

The first thing you need to do to get started is to create a HTML form on a web page. for the purpose of this tutorial the all of the code will run together on the same page instead of using multiple pages. I am assuming you understand basic HTML so I will  not be explaining everything you may need to know about HTML to use this tutorial. In the following HTML code there is a Form, a table, two text input boxes (1 for a username and 1 for a password) and a submit button.It is inportant to use unique names for each of the input boxes as they will be used when retrieving the information which has been submitted.

<form name="regForm" action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
    <table>
        <tr>
            <td>Username:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td colspan="2"><center><input type="submit" name="submit" value="Sign Up" /></center></td>
        </tr>
    </table>
</form>

You may need to change the action from:

action="<?php $_SERVER["PHP_SELF"] ?>"

to

action="/YourPHPFormHere.php"

If you are using the current php file, you can leave delete the action tag as it will default to itself.

 

Handling the PHP

The next thing we will be doing is creating the php which we will be using to read the contents of the form. If you are doing this in the same page then you will need to include this at the top of the page in PHP tags before any of the HTML.

<?
if (isset($_POST['submit'])){
    echo "Form was Submitted Successfully<br>";
    echo "Username = ". $_POST['username'] . "<br>";
    echo "Password = ".$_POST['password'];
}
?>

So to understand what is going on, we need to have a look the POST Array. If you havent done about Arrays yet then I would recommend checking out our previous tutorial here. When the form is submitted, it creates an array within the HTTP headers with the contents of the items inside the form and their values. To view the full contents of the POST Array, you can do so by printing it out at the top of the page. If you where to do this without submitting the form, these are the results you would get back:

Array ( )

So from this, it looks like the array is empty. If we where to then enter text in to each of the input fields, we should see the following been returned

Array
(
    [username] => My Username
    [password] => My Password
    [submit] => Sign Up
)

So to actually check if the forms submit button has been checked, we have included the following line of code:

if (isset($_POST['submit'])){
    //Code which runs when the form has been submitted
}

This works by using the isset function to check if the $_POST['submit'] has been set (checks if the button has been clicked).

The next lines work by echoing the contents of the POST arrays value. You will access this by doing

$_POST['Your Input Boxes Name']

So we have used:

echo "Form was Submitted Successfully<br>";
echo "Username = ". $_POST['username'] . "<br>";
echo "Password = ".$_POST['password'];

At present, the finished page should look like this:

<?
if (isset($_POST['submit'])){
    echo "Form was Submitted Successfully<br>";
    echo "Username = ". $_POST['username'] . "<br>";
    echo "Password = ".$_POST['password'];
}
?>

<html>
<head>
    <title>PHP4Beginners P10: Passing Variables P1 POST</title>
</head>
    <body>
        <center>
            <h1>PHP For Beginners P10: Passing Variables P1 POST</h1>
            
            <form name="regForm" action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
                <table>
                    <tr>
                        <td>Username:</td>
                        <td><input type="text" name="username"></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="password"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><center><input type="submit" name="submit" value="Sign Up" /></center></td>
                    </tr>
                </table>
            </form>
        </center>
    </body>
</html>

Now, if we where to submit the form with the Username as Fred and the Password as MyPassword123, we should see the following:

 

Attached File  html form.JPG   22.46KB   0 downloads

 

If you then wanted to have it so that you do not see the html page after, you would simply and an else statement so if the form has not been submitted then you will only see the HTML and if you have submitted it then you will not see the HTML page. The code for this page would be

<?
if (isset($_POST['submit'])){
    echo "Form was Submitted Successfully<br>";
    echo "Username = ". $_POST['username'] . "<br>";
    echo "Password = ".$_POST['password'];
}else{
?>

<html>
<head>
    <title>PHP4Beginners P10: Passing Variables P1 POST</title>
</head>
    <body>
        <center>
            <h1>PHP For Beginners P10: Passing Variables P1 POST</h1>
            
            <form name="regForm" action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
                <table>
                    <tr>
                        <td>Username:</td>
                        <td><input type="text" name="username"></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="password"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><center><input type="submit" name="submit" value="Sign Up" /></center></td>
                    </tr>
                </table>
            </form>
        </center>
    </body>
</html>
<?}?>

You can download the source code for this in the attachments

Attached Files







Also tagged with one or more of these keywords: PHP4Beginners