Search for a tutorial

User submitted
• User navigation
• Other content

we will write two PHP pages: One script will contain an HTML form that can be submitted; the other will be the action page, which is the script the form is submitted to. When a form submits to another page, PHP makes the data from the form avaliable to the action page. The first PHP script will be named who_are_you.php and is a simple HTML page that contains a form. Type the code from the following listing below and save to your web server:

Listing: Basic-Data-Entry page (WHO_ARE_YOU.PHP)

<html>
<head>
<title> Who are you?</title>
</head>
<form action="you_are.php">
Please enter your name:<br />
I am....
<?php print('<input type="text" name="person" value=" ' . $person .'"size="15" />');
?>
<input type="submit" value="Go!" size="15" />
</form>
</body>
</html>

                                

The script creates an ordinary HTML form, which is submitted to you_are.php (the URL in the forms action attribute). The form contains a single text input field and a submit button. When the user clicks the button, the form is sent to the action page.

The value for the text input is generated by the concatenating - that is, "Gluing" a string together with a variable and some string literals. The PHP string-concatenation operator is a period ( . ); it tells the interpreter to take the preceding and following items and connect them into a single string. The notation:

.... value=" ' . $person . '"....

populates the value and attribute with the variable $person, which we create to store the data were prompting the user to enter.

Forms and QUERY STRINGS
Now that we have created a PHP script that generates an HTML form, we must create the action page for the form to submit to. The HTML form submits to the page you_are.php. Create a PHP script named you_are.php and enter the information/code in the listing below

Listing: Basic-Action- Page (YOU_ARE.PHP)

<html>
<head>
<title> You are!.....</title>
</head>
<body>
<?php 
print(' Well, Hello ' . $person . ' , nice to meet you! ');
print('<br />');
print('<a href="who_are_you.php?person=' . urlencode($person) . '">
Back to Who Are You?</a>');
?>
</body>
</html>
                                

There you have it , you have learnt how to create a basic form posting script.

Thanks for reading the tutorial, please view some of the other tutorials we have to offer.