Strickly speaking Forms are not PHP they are HTML, Form Handling however can be done in PHP having said that in order for you to get any input from the visitor you need to understand Forms and how they communicate with PHP script on the server. So in this Part of this Free online PHP training course we will have a look at Forms and Form Handling using PHP.

Lets setup a form that will get visitors Firstname and Lastnameand will return the full name. getting the First name and Last name from the visitor directly, for this you need a HTML form here would be a code that you could use in a HTML file:

<form action=”form_handling.php” method=”post”>
<label for=”firstname”>Enter Firstname</label><input type=”text” name=”firstname”><br />
<label for=”lastname”>Enter Lastname</label><input type=”text” name=”lastname”><br />
<input type=”submit” value=”submit” name=”submit”>
</form>

now for handling the form data create a new file called “form_handling.php” and here is a code for it:

<?php

$first_name=$_POST[“firstname”];// get the firstname from the form

$last_name=$_POST[“lastname”];//get the lastname from the form

echo “Your name is : “.$first_name.” “.$last_name;// display the result

?>

now the code above is the simplified version, don’t get me wrong it will work but if for example user enters numbers or question mark or nothing, it will still show as Your name is : ?><:@: and that is something we don’t want so we have to check for it to be the correct information entered this is form validation and will be discussed in another lesson for now I will do some server side check(because this is a PHP tutorial otherwise I would use JavaScript as it would make more sense).

So still working with “form_handling.php” and here is the code:

<?php

$first_name=$_POST[“firstname”];// get the firstname from the form

$last_name=$_POST[“lastname”];//get the lastname from the form

//————————checking FUNCTION starts here————————————–

function checking_data($data){

$result=false;

if($data==””){// if it is empty do nothing

}else{

$second_check=true;// this is a variable indicating that it is A-Z

$d=strtoupper($data);// change all of it to upper case so you only have to check it once

for($i=0 ; $i <strlen($data); $i++){

if(ord($d[$i])>=65 and  ord($d[$i])<=90){

//if this letter is not in alephabet then make it false

}else{

$second_check=false;

}

}

if($second_check){

$result=true;

}

return $result;

}

}

if(checking_data($first_name) and checking_data($last_name)){// check both fields

echo “Your name is : “.$first_name.” “.$last_name;// display the result

}else{

echo “Please enter first name and Last name. use the back on the browser to go back to the form”;//display an error that they havn’t enter the right information into the form

}

?>

I know it’s a very long code but it’s better than looking like an idiot, right? Ok now you can put both of the form and the code in one single file and here is how you do it:

Create a file and lets call it form.php and here would be the code for it:

<form action=”form.php” method=”post”>
<label for=”firstname”>Enter Firstname</label><input type=”text” name=”firstname”><br />
<label for=”lastname”>Enter Lastname</label><input type=”text” name=”lastname”><br />
<input type=”submit” value=”submit” name=”submit”>
</form>

<?php

//—————-function————————

function checking_data($data){

$result=false;

if($data==””){// if it is empty do nothing

}else{

$second_check=true;// this is a variable indicating that it is A-Z

$d=strtoupper($data);// change all of it to upper case so you only have to check it once

for($i=0 ; $i <strlen($data); $i++){

if(ord($d[$i])>=65 and  ord($d[$i])<=90){

//if this letter is not in alephabet then make it false

}else{

$second_check=false;

}

}

if($second_check){

$result=true;

}

return $result;

}

}

if(isset($_POST[“submit”])){// if the form has been submited

$first_name=$_POST[“firstname”];// get the firstname from the form

$last_name=$_POST[“lastname”];//get the lastname from the form

if(checking_data($first_name) and checking_data($last_name)){// check both fields

echo “Your name is : “.$first_name.” “.$last_name;// display the result

}else{

echo “Please enter first name and Last name. use the back on the browser to go back to the form”;//display an error that they havn’t enter the right information into the form

}

}

 

?>

Please note that the form action had to be changed in order for it to send the form data to the right php code. The result of the above code is a form that will ask for firstname and lastname and will return the full name and will check to see if the user have entered the right information or not and all of them in one php file.