How to Get User Information with PHP - Use PHP to Ask a Web Page User Questions and Process the Answers

Many 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 user name and password of the user (when logging in)
  • Details about the user when registering (such as the proposed user name and password)
  • Details about products when ordering on line (such as a discount voucher number)

The information gathering process consists of two stages:

  • The web page presents an HTML form to the user. The form will contain input boxes, radio button and drop down lists to illicit the required information from the user
  • PHP code then processes the information entered by the user. For example the code may check whether a required username already exists and creates it if it doesn’t.

The first step is, therefore, to create the form for the user data entry.

Creating a HTML Form with PHP

A 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:

echo "
Register new user:
First Name: Surname:
User name:
Password: Repeat Password:
"
; } show_form();


In this case the user is asked for:

  • Their first name and surname
  • A potential user name
  • A password (which must be entered twice to ensure that it is spelled correctly)

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 PHP

The 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
"; } if (($_REQUEST['password1']=="")||($_REQUEST['password2']=="")) { $errors .= "Password and repeat passwords are required
"; } if ($_REQUEST['password1'] != $_REQUEST['password2']) { $errors .= "Password and repeat passwords must match
"; }


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
"; 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.

Summary

A 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.



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center