Czech version
logolink

< Back to the list of lessons

PHP and Forms II

PHPContent of the lesson:

  • Registration Form
  • Acquiring Data from the Registration Form
  • Individual Task
  • Homework

Registration Form

The submit button was missing in the form from the previous lesson (you were not able to send any data). Now we are going to process the data from this form and implement the functionality to the form (check, evaluation, data processing).

The task of this lesson is to create the file registracni-formular.php which will contain our defined form and display input information after submitting the form.

Completing the Registration Form

Complete the form by adding the submit button. You can use the HTML element input: <input type="submit" value="Send the form" />. This element will be displayed like the following button: .

Clicking on this button will run the following actions:

  1. Gathering data from the form.
  2. Sending data vid HTTP protocol to server (the details of transfer can differ according to the selected method - GET/POST).
  3. Giving transferred data to the PHP script written in attribute action. This script will generate the result page and send it back to our browser.

The whole process can be easily described: "After the button was clicked take all input information and send it to the script inside registracni-formular.php which will process it and generate a HTML page which will be the result of this processing."

We will use the method GET (inside the URL address) and send data to the script inside registracni-formular.php.

HTML code for the registration form

Registration form source code

<form action="registracni-formular.php" method="get" id="registracni-formular">
<p><label for="jmeno">First Name</label><br /><input name="jmeno" id="jmeno" type="text"></p>
<p><label for="prijmeni">Last Name</label><br /><input name="prijmeni" id="prijmeni" type="text"></p>
<p><label for="e-mail">E-mail</label><br /><input name="e-mail" id="e-mail" type="text"></p>
<p><label for="pohlavi">Sex</label><br />
<select name="pohlavi" id="pohlavi">
<option value="muz">Male</option>
<option value="zena">Female</option>
</select>
</p>
<p>
<label for="komentar">Comment</label><br>
<textarea name="komentar" type="textarea" cols="30" rows="5" id="komentar"></textarea>
</p>
<p>Favorite Topics<br>
<input name="automobily" value="Automobily" id="automobily" type="checkbox">
<label for="automobily">Cars</label><br>
<input name="moda" value="Móda" id="moda" type="checkbox"><label for="moda">Fashion</label><br>
<input name="pocitace" value="Počítače" id="pocitace" type="checkbox"><label for="pocitace">Compuers</label>
</p>
<p><input name="odeslat-formular" type="submit" value="Send" /></p>
</form>

The final appearance of the registration form inside a browser






Favorite Topics


Acquiring Data from the Registration Form

The file registracni-formular.php is still a static HTML file which contains the form. Now we will add the PHP code which will get all required data and display it again. Insert the following code behind the <body> element:

Acquiring data using the method GET

<?php

if ($_GET["odeslat-formular"]=="Send"){

$jmeno = $_GET["jmeno"];
$prijmeni = $_GET["prijmeni"];
$email = $_GET["email"];
$pohlavi = $_GET["pohlavi"];
$komentar = $_GET["komentar"];

print("Entered first name:".$jmeno."<br />");
print("Entered last name:".$prijmeni."<br />");
print("Entered e-mail:".$email."<br />");
print("Entered sex:".$pohlavi."<br />");
print("entered comment:".$komentar."<br />");
}
?>

The condition if ($_GET["odeslat-formular"]=="odeslat"){ asks the question "Was the submit button pressed?". More precisely: "Is there an element with index "odeslat-formular" and value "odeslat-formular" inside the superglobal array GET?".

Now we have to answer the question how to send data from the browser inside the PHP script. The server creates a superglobal array (predefined array which contains values of variables used inside the page). Such an array is superglobal which means "visible" for the whole script. You can access this array via the variable $_GET. Our associative array inside the $_GET variable looks like this one:

odeslat-formular jmeno prijmeni email pohlavi ...
Semd data inserted to the element with attribute name=jmeno data inserted to the element with attribute name=prijmeni data inserted to the element with attribute name=email data chosen inside the select element with attribute name=pohlavi ...

After sending our form back to server this array will be filled with values:

odeslat-formular jmeno prijmeni email pohlavi ...
Send František Žebř frantisek.zebr@centrum.cz Male ...

The superglobal array $_GET contains the element only when it is a part of the form. In case there is an element $_GET["odeslat-formular"] inside the form and has the value of "Send" then you know the button was pressed and you should process the form.

Individual Task

Find out using the Internet how to process items from the checkbox elements (and any other). You can download the form here: registracni-formular.php.

Homework

1. Expand the current form using these details: street, number of house, city, postal code, password, help for password and a question whether the user wants to receive advertisement (answer is yes or no, you cannot use the element SELECT).

2. Using the Internet find out how to acquire data from the form using a better way than simple assignment ($jmeno-formularoveho-elementu = $_GET["jmeno-formularoveho-elementu"];). Help: Does the form write something like "Notice: Undefined index: odeslat-formular in ...\registracni-formular.php on line 12" to you? Why does it write? Can it be solved?

Additional Texts

Links

Questions

  1. Which tag is used for sending a form?
  2. How can you use the attribute action?
  3. What happens when you click on the submit button?
  4. How to acquire the sent data inside the PHP script?
  5. What is the difference between processing data which were sent using the GET or POST method?
webdesign, xhtml, css, php - Mgr. Michal Mikláš