|
||||||
How to Get User Information with PHPUse PHP to Ask a Web Page User Questions and Process the AnswersMany web pages require their users to input data. The questions can be asked and the answers processed by using some simple PHP code.
The web site designer often needs to obtain information from the web site user. For example they may need to know:
The information gathering process consists of two stages:
The first step is, therefore, to create the form for the user data entry. Creating a HTML Form with PHPA form is simply the method by which the web site programmer asks the user questions and can be defined using HTML. However, the programmer will find that a more flexibly and adaptable application can be created if the form is created by using a PHP function: function show_form() {
echo "<form method=post>
Register new user:
<table>
<tr><td>First Name:<input name=firstname></td>
<td>Surname:<input name=surname></td></tr>
<tr><td colspan=2>User name:<input name=username></td></tr>
<tr><td>Password:<input name=password1 type=password></td>
<td>Repeat Password:<input name=password2 type=password></td></tr>
<tr><td colspan=2 align=center>
<input type=submit value='Submit New User Details'></td></tr>
</table>
</form>";
}
show_form();
In this case the user is asked for:
If this is saved into a PHP file then the user will be able to view the form via a web browser. It is now ready for the next stage – the processing of the input data. Processing Data Using PHPThe programmer can write a second function to process the data entered by the user: function check_data () {
The purpose of this function is to ensure that the data entered by the user is valid and is then processed correctly. It does this by making use of the $_REQUEST array. The $_REQUEST array is an associative array and contains all of the data sent from the form by the user: $errors = "";
if ($_REQUEST['username']=="") {
$errors .= "User name is required<br>";
}
if (($_REQUEST['password1']=="")||($_REQUEST['password2']=="")) {
$errors .= "Password and repeat passwords are required<br>";
}
if ($_REQUEST['password1'] != $_REQUEST['password2']) {
$errors .= "Password and repeat passwords must match<br>";
}
And the application must allow the user to correct any mistakes: if ($errors != "") {
echo $errors;
show_form();
}If everything has been entered correctly then the application may carry on and process the data appropriately. In this case it will add the new user to the database: $db = new mysqli ("localhost", "root", "root_password", "mysql");
$sql = "select * from user
where user='" . $_REQUEST['username'] . "'";
$rs = $db->query($sql);
if ($rs->num_rows > 0 ) {
echo "User already exists<br>";
show_form();
} else {
$sql = "grant select on * to " . $_REQUEST['username'] . "@localhost
identified by '" . $_REQUEST['password1'] . "'
";
$db->query($sql);
echo "User " . $_REQUEST['username'] . " created";
}
$rs->close();
$db->close();
}
Of course, the function should only be called if the user has actually entered the information: if (isset($_REQUEST['username'])) {
check_data();
} else {
show_form();
}
Now the form will only appear when the user first accesses the web page or if they then make an invalid entry. Once they submit the form then the PHP data processing will take over. SummaryA PHP programmer uses an HTML form to obtain data from their application users. When the user submits the form then the application receives all of the data in an associative array - $_REQUEST. The application can then process the data to ensure that it is valid and is handled appropriately.
The copyright of the article How to Get User Information with PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish How to Get User Information with PHP in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||