Let’s create a simple web page where a user will give his first and last name and we’ll display his full name (by concatenating first and last name) in some other web page.
So we’ll make two files – one HTML file and one PHP file. HTML file will be for designing the layout where user will enter his first and last name and PHP file will be for generating logic to display his full name at once.
The HTML file I have designed is something like below –
<html>
<head>
<title>FIRST AND LAST</title>
</head>
<body text="darkgreen">
<br><br><br>
<form name="myform" action="firstand lastname.php" method="post">
<fieldset>
<legend>USER INFO</legend>
<table>
<tr> <td>First Name</td><td><input type="text" name="first"></td> </tr>
<tr> <td>Last Name</td><td><input type="text" name="last"></td> </tr>
<tr> <td><input type="submit" name="submit" value="SUBMIT"></td> </tr>
</table>
</fieldset>
</form>
</body>
</html>
Please take note of this that whenever we place any submit button in form, it is compulsory to use <form> tag. So in above code inside <form> tag I have used action=” “ . In action I have given the name of my php file which contains logic to display full name. You may give name of your PHP file inside action. So the PHP code I wrote in my PHP file is-

In PHP code, I have first extracted the keys into variables. (Actually the keys are the name which we give inside <input> tag in HTML file. You may check this using var_dump($_POST)) .
So converting keys into variables allows us to handle them as normal variables in PHP. Using isset($submit) tells us whether SUBMIT button is pressed or not. (submit is the name of my SUBMIT button in my HTML code, if you have given some other name use that name) . If SUBMIT is pressed then the first and last name will be concatenated with a space in between. And we will simply echo this. ($first and $last are my name which I have given to first and last name respectively in my html code, yours might be different so use that)
Save both your PHP and HTML file in Documents Tool Folder (www) and run your HTML file (not PHP).
That’s it.
Have ERROR FREE coding!
