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 "<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:
- 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
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @alv! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the total payout received
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @alv I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit