0% found this document useful (0 votes)
27 views

12-PHP and Web Forms

The document discusses PHP and handling web forms. It explains that the $_GET and $_POST variables are used to retrieve form data submitted by users. An example form is provided that sends name and age data to a PHP file called welcome.php. This file then uses $_POST to display the submitted name and age. The document also notes that user input should be validated on the server for security reasons. An exercise is provided to create a poll form that submits color choice data to a results page.

Uploaded by

Eric Nilo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

12-PHP and Web Forms

The document discusses PHP and handling web forms. It explains that the $_GET and $_POST variables are used to retrieve form data submitted by users. An example form is provided that sends name and age data to a PHP file called welcome.php. This file then uses $_POST to display the submitted name and age. The document also notes that user input should be validated on the server for security reasons. An exercise is provided to create a poll form that submits color choice data to a results page.

Uploaded by

Eric Nilo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PHP and Web Forms

John Ryan B. Lorca Instructor I

PHP and Web Forms


The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.

PHP and Web Forms


The example HTML page above contains two input fields and a submit button. When the user fills in this form and click on the submit button, the form data is sent to the "welcome.php" file.
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
3

PHP and Web Forms


The "welcome.php" file looks like this:
<html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html> A sample output of the above script may be:
Welcome John. You are 28 years old.

The PHP $_GET and $_POST variables will be fully explained in the next lecture.
4

PHP and Web Form


User input should be validated whenever possible. Client side validation is faster, and will reduce server load. However, any site that gets enough traffic to worry about server resources, may also need to worry about site security. You should always use server side validation if the form accesses a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.
5

Exercise
Create a page named index.php On the page, create a poll-form with three options (radio buttons) The poll-question will be, What is your favorite color? The options will be red, white, or blue Create a page named result.php On the page, display the result in the format below: Your favorite color is red.
6

You might also like