How to Make Form Input in PHP

How to make form input in php - Input form created with HTML tags. Pages containing pure form (no scriptphp) do not have to be stored in php form, can be in html form.
To design an input form, there are at least 3 (three) important things, namely:
1. METHOD
The method of a form determines how the form data input is sent. This method there are two kinds, namely GET and POST. This method determines how the input data is sent and processed by PHP.
2. ACTION
The action of a form determines where the input data of the form is processed. If this action is emptied, then it is considered that the form process takes place on the same page. So the form page and the process page can be separated or made into one.
3. SUBMIT BUTTON
Submit button is a button (in general) that serves as a trigger for sending data from form input. If this button is pressed, then the form data will be sent (processed) on the page specified in the action attribute.
How to make form input in php
Method 1: Unify the Form and Process
The form processing process is done on the same page as the inputan form. If the form handling process is on one page, then the value of the action attribute on the form tag does not need to be filled (emptied).
Program 1
File Name: input01.php
Description: Program Example of form processing where between form input and processing process of input in one page.
Open notepad, then type the following html code.
<html>
<head><title>Form Input</title></head>
<body>
  <FORM ACTION="" METHOD="POST" NAME="input">
   Your name : <input type="text" name="name"><br>
<input type="submit" name="Input" value="Input">
</FORM>
</body>
</html>

<?php
if (isset($_POST['Input'])) {
$name = $_POST['name'];
 echo "Your name : <b>$name</b>";
}
?>

How to Make Form Input in PHP

Save the code into the htdocs folder named input01.php.
Program Explanation 1
Some things to note from the program 4.1 above, among which the name of each component of the form because this name will be the index array in PHP. In the above 4.1 program, the value of the action attribute in the form tag is not filled (line 4), this means that the form processing is on the same page. Next (line 4) method used in form handling is POST. This method is preferred in handling an input form. Starting line 11 to 16, there is a script / PHP program that will handle (process) the value entered through the form. The location of this input process may be before or after the form, depending on the needs. On the 12th line, there is a check of the condition of whether the button with the name "Input" (note the $_POST index array and compare with the button name submit on form) is completely suppressed or not by the user. Next on line 13, the input value of the form will be retrieved by accessing the $_POST array according to the component of the form to be retrieved. In line 13, the contents of the input component with the name "name" will be retrieved and entered into the $name variable. The $_POST array index on line 13 must be the same as the value of the name attribute on line 5.
To see the result open the browser then in the address bar type http://localhost/input01.php. Then the result will look like the following.
How to Make Form Input in PHP
Program View

Method 2: Separate between Form and Process
The form processing is done in separate pages with the form input. If the form handling process is done on a different page, then the value of the action attribute in the form tag must be filled with the address of the page where the form processing process.
Program 2a
File Name: input02.php
Description: The program displays the input form with the POST method
Open notepad, then type the following php code.
<html>
<head><title>Processing Form</title></head>
<body>
  <FORM ACTION="proses02.php" METHOD="POST" NAME="input">
   Your name : <input type="text" name="name"><br>
   <input type="submit" name="Input" value="Input">
</FORM>
</body>
</html>

How to Make Form Input in PHP

Save the code into the htdocs folder named input02.php.
Program 2b
File Name: process02.php
Description: Input form handling program for Program 2.
Open notepad, then type the following php code.
<?php
if (isset($_POST['Input'])) {
$name = $_POST['name'];
 echo "Your Name : <b>$name</b>";
}
?>

How to Make Form Input in PHP

Save the code into the htdocs folder named process02.php.
To see the result open the browser then in the address bar type http://localhost/input02.php. Then the result will look like the following.
How to Make Form Input in PHP
Program View

How to Make Form Input in PHP
Program View

Program 3a
File Name: input03.php
Description: The program displays the input form with the GET method.
Open notepad, then type the following php code.
<html>
<head><title>Processing Form</title></head>
<body>
<FORM ACTION="proses03.php" METHOD="GET" NAME="input">
   Your name : <input type="text" name="name"><br>
   <input type="submit" name="Input" value="Input">
</FORM>
</body>
</html>

How to Make Form Input in PHP

Save the code into the htdocs folder named input03.php.
Program 3b
File Name: process03.php
Description: Input form handling program for Program 3a.
Open notepad, then type the following php code.
<?php
if (isset($_GET['Input'])) {
$name = $_GET['name'];
 echo "Your Name : <b>$name</b>";
}
?>

How to Make Form Input in PHP

Save the code into the htdocs folder named process03.php.
Explanation of Programs 2a, 2b, 3a and 3b
Program 2a and program 2b are in principle the same as program 1. The results are the same. However, the process of handling the form (PHP) is separate from the inputan form (HTML). In programs 2a and 2b, form handling uses the POST method, while in programs 3a and 3b, form handling is done by the GET method. Notice, this method determines how a form is processed and how the input variable is taken. Compare the 2nd and 3rd rows of the 2b and 3b programs!
To see the result open the browser then in the address bar type http://localhost/input03.php. Then the result will look like the following.
How to Make Form Input in PHP
Program View

How to Make Form Input in PHP
Program View

That is a tutorial that I can share with you about how to make form input in php, hopefully can be useful and can improve our ability in understanding the php programming language. See also article on: Break and Continue in For Loop PHP.

Comments