​How Can PHP Accept Data from a Form and Assign It to Variables for Future Retrieval?

Problem scenario
You want a web page to request user input.  After the user clicks "Submit" you want the values to be assigned to variables for future usage (e.g., an algorithm could retrieve the values to manipulate them).  How do you use PHP to assign input from a web page to be variables on the back end?

You need two files.  The first one will be a form to obtain data from an external source (user-entered data).  You need a PHP web page like this (cool.php) to obtain data passed in:

<form action="contint.php" method="post">
    Username:  <input type="text" name="username" /><br />
    Password: <input type="text" name="password" /><br />
    <input type="submit" name="submit" value="Submit me!" />

//Change contint.php to the name of the second .php file you will use.

Here is the content of the second .php file you will use (e.g., contint.php)

if ($_POST) {
    echo '<pre>';
    $var1 = $_POST["username"];
    $var2 = $_POST["password"];
    echo '</pre>';

//Now $var1 is the value the user entered in "Username."  
//Now $var2 is the value the password entered in "Password."

Leave a comment

Your email address will not be published. Required fields are marked *