Form and PHP code
Methods to send Form data
There are two methods to send user inputs to script:
POST method is a safer method as the inputs will be send by attaching to the trail of the HTTP protocol.
- GET
- POST
POST method is a safer method as the inputs will be send by attaching to the trail of the HTTP protocol.
Data passed from FORM
$_GET is an array contains values passed using GET method. Syntax: $_GET [name] where name is the name attribute of form element$_POST is an array contains values passed using POST method. Syntax: $_POST [name] where name is the name attribute of form element
Options to handling FORM with PHP:
- Separated form and PHP code.
- PHP code and form in a single PHP file
Separated form and PHP code.
The easiest way to handle user input is used separated form and script. In this case, you need to create a form in HTML file. And, in the action attribute specify the PHP file that handle input from this file.
form.html
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
process.php
<?php
echo $_GET['age'];
?>
form.html
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
process.php
<?php
echo $_GET['age'];
?>
PHP code and form in a single PHP file
In this option, the PHP file consists of two roles: display form and process the input. PHP file has to determined when to display a form and when to process the input. It is very critical as the PHP code cannot process the task if there is no data from the form. Thus, we need to check whether the user has submitted the form. If the user did not submit form, display form for user input. If the user has filled in the form and pressed the submit button, Then, PHP will process the inputs from user.
registration.php
<?php
if(isset($_POST['submit'])){
// process the task as the submit button is pressed
}else{
// Display the form if the submit is not detected.
?>
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
<?php
}
?>
registration.php
<?php
if(isset($_POST['submit'])){
// process the task as the submit button is pressed
}else{
// Display the form if the submit is not detected.
?>
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
<?php
}
?>
No comments:
Post a Comment