How to use HTML Form in PHP

Monday, August 17, 2009 · Posted in ,

Form is one of the most important tool in making and designing a website or web application. Through the use of form you can send any values to another page. try to observe the given sample code below.



HTML Code:




<html>

<head>

<title>Title goes heretitle>

head>

<body>



<form action="welcome.php" method="post">



<label>Name:label><input type="text" name="name"><br/>

<label>Age:label><input type="text" name="age"><br/>

<input type="submit" name="btn_submit">



form>



body>

html>



The page contains two input fields and a submit button in which it has a type of text and a name of name and age.



Sample Output 1:


and if we were going to run the code above and then this would be the output of the code that we have made above in which it has a two input fields in which this field will be use in getting the value that comes from the user and also the submit button.



HTML with PHP Code:

<html>

<head>

<title>Title goes heretitle>

head>

<body>



Welcome echo $_POST['name']; ?>.<br/>

You are echo $_POST['age']; ?> years old.



body>

html>

and this would be our another page in which it has a filename of welcome.php. As you notice that its filename is being put at the from action so that when the user click the submit button and then it pass the value the being input by the user through the use of the form and then the form will past the value the the welcome.php and will directly display the value in the page. As you notice in the code we embed the php code in the HTML code. In this code we have the opening and the closing PHP tag. The variable array $_post["name"]; and $_post["age"]; are the two variables that holds the value that came from the form and this variable also uses to display the value that being input in the form.


Sample Output 2:








Powered by Blogger.