0% found this document useful (0 votes)
38 views

Internet Applications: PHP Arrays, HTML Forms

The document discusses HTML forms and how they are used with PHP. It covers: - HTML forms allow collecting data from clients and sending it to backend applications like PHP for processing. - The <form> tag is used to create an HTML form and includes attributes like action and method. - The method attribute specifies how data is uploaded, with GET putting data in the URL and POST sending it hidden headers. - PHP stores submitted form data in the $_GET, $_POST, and $_REQUEST arrays. - Different form controls like text, checkboxes, selects are used to collect various types of data.

Uploaded by

Ashok Sairam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Internet Applications: PHP Arrays, HTML Forms

The document discusses HTML forms and how they are used with PHP. It covers: - HTML forms allow collecting data from clients and sending it to backend applications like PHP for processing. - The <form> tag is used to create an HTML form and includes attributes like action and method. - The method attribute specifies how data is uploaded, with GET putting data in the URL and POST sending it hidden headers. - PHP stores submitted form data in the $_GET, $_POST, and $_REQUEST arrays. - Different form controls like text, checkboxes, selects are used to collect various types of data.

Uploaded by

Ashok Sairam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Internet Applications

PHP Arrays, HTML Forms

Instructor: Ashok Singh Sairam


[email protected]
PHP - Dealing with the Client
• PHP allows you to use HTML forms
• Forms collect data from the client and send it to the backend
application for processing
• Forms require technology at the server to process them
• PHP is a feasible and good choice for the processing of HTML forms
Anatomy of HTML form Tag
• The HTML form tag is used to create an HTML form

<form action = "Script URL" method = "GET|POST">


form elements like input, text area etc.
</form>

MA 518: Database Management Systems 3


Anatomy of HTML form Tag
• The HTML form tag is used to create an HTML form
• action: backend script to process data passed from the client

<form action = "Script URL" method = "GET|POST">


form elements like input, text area etc.
</form>

MA 518: Database Management Systems 4


Anatomy of HTML form Tag
• The HTML form tag is used to create an HTML form
• action: backend script to process data passed from the client
• method: The method specifies how the data will be uploaded
• GET method sends all form input in the URL requested, using name=value pairs
separated by ampersands (&)

<form action = "Script URL" method = "GET|POST">


form elements like input, text area etc.
</form>

MA 518: Database Management Systems 5


Anatomy of HTML form Tag
• The HTML form tag is used to create an HTML form
• action: backend script to process data passed from the client
• method: The method specifies how the data will be uploaded
• GET method sends all form input in the URL requested, using name=value pairs
separated by ampersands (&)
• E.g. process.php?name=trevor&number=345
• Is visible in the URL shown in the browser

<form action = "Script URL" method = "GET|POST">


form elements like input, text area etc.
</form>

MA 518: Database Management Systems 6


Anatomy of HTML form Tag
• The HTML form tag is used to create an HTML form
• action: backend script to process data passed from the client
• method: The method specifies how the data will be uploaded
• GET method sends all form input in the URL requested, using name=value pairs
separated by ampersands (&)
• E.g. process.php?name=trevor&number=345
• Is visible in the URL shown in the browser
• POST method sends all contents of a form with basically hidden headers (not easily
visible to users)

<form action = "Script URL" method = "GET|POST">


form elements like input, text area etc.
</form>

MA 518: Database Management Systems 7


$_GET and $_POST
• All form values are placed into an array
$_GET and $_POST
• All form values are placed into an array
• PHP loads the values for the URL parameters into an array called $_GET and the
POST parameters into an array called $_POST
$_GET and $_POST
• All form values are placed into an array
• PHP loads the values for the URL parameters into an array called $_GET and the
POST parameters into an array called $_POST
• There is another array called $_REQUEST which merges GET and POST data
Example: $_GET and $_POST
• Assume a form contains one <form action=“process.php" method="post" >
textbox called “txtName”
<input type=“text" name=“txtName">
and the form is submitted </form>
using the post method,
invoking process.php
Example: $_GET and $_POST
• Assume a form contains one <form action=“process.php" method="post" >
textbox called “txtName”
<input type=“text" name=“txtName">
and the form is submitted
</form>
using the post method,
invoking process.php
• process.php could access the
form data using:
• $_POST[‘txtName’]
Example: $_GET and $_POST
• Assume a form contains one <form action=“process.php" method=“get" >
textbox called “txtName” and
the form is submitted using <input type=“text" name=“txtName">
</form>
the post method, invoking
process.php
• process.php could access the
form data using:
• $_POST[‘txtName’]
• If the form used the get
method, the form data would
be available as:
• $_GET[‘txtName’]
HTML Forms: A simple example
<form action="" method="post" id="numbers">
<p> Enter Your name: <input type = “text” name =
“my_name”> <p>

<input type="submit" value="Submit" name="submit">


</form>
HTML Forms: A simple example
<form action="" method="post" id="numbers">
<p> Enter Your name: <input type = “text” name =
“my_name”> <p>

<input type="submit" value="Submit" name="submit">


</form>

<?php
echo '$_GET value is: ';
print_r($_GET);
echo "<br>";
echo '$_POST value is:';
print_r($_POST);
?>
HTML Forms: A simple example
<form action="" method="post" id="numbers">
<p> Enter Your name: <input type = “text” name =
“my_name”> <p>

<input type="submit" value="Submit" name="submit">


</form>

<?php
echo '$_GET value is: ';
print_r($_GET);
echo "<br>";
echo '$_POST value is:';
print_r($_POST);
?>
HTML Forms: A simple example (2)
<form action="" method=“get" id="numbers">
<p> Enter Your name: <input type = “text” name =
“my_name”> <p>

<input type="submit" value="Submit" name="submit">


</form>

<?php
echo '$_GET value is: ';
print_r($_GET);
echo "<br>";
echo '$_POST value is:';
print_r($_POST);
?>
HTML Forms: A simple example (3)
<form action="" method="post" id="numbers">
<p> Enter Your name: <input type = “text” name =
“my_name”> <p>

<input type="submit" value="Submit" name="submit">


</form>

<?php
echo '$_GET value is: ';
print_r($_GET);
echo "<br>";
echo '$_POST value is:';
print_r($_POST);
?>
HTML Form Controls
• Different type of form controls use to collect data using HTML form
• Text Input Controls
• Hidden Controls
• Submit and Reset Button
• Checkboxes Controls
• Radio Box Controls
• Select Box Controls
• File Select boxes
• Clickable Buttons

Courtesy: https://www.tutorialspoint.com/html/html_forms.htm

MA 518: Database Management Systems 19


Text Input Controls
• <input> tag is used for items that require only one line of user input,
such as search boxes or names

<form action = "Script URL" method = "GET|POST">


First name: <input type = "text" name = "first_num“ id="first_num"
required="required" value="<?php echo $first_num; ?>" />
</form>

MA 518: Database Management Systems 20


Text Input Controls
• <input> tag is used for items that require only one line of user input,
such as search boxes or names
• type: indicates the type of input control – text, number, password

<form action = "Script URL" method = "GET|POST">


First name: <input type = "text" name = "first_num“ id="first_num"
required="required" value="<?php echo $first_num; ?>" />
</form>

MA 518: Database Management Systems 21


Text Input Controls
• <input> tag is used for items that require only one line of user input,
such as search boxes or names
• type: indicates the type of input control – text, number, password
• name: give a name to the control, can be used at server side to get the value

<form action = "Script URL" method = "GET|POST">


First name: <input type = "text" name = "first_num“ id="first_num"
required="required" value="<?php echo $first_num; ?>" />
</form>

MA 518: Database Management Systems 22


Text Input Controls
• <input> tag is used for items that require only one line of user input,
such as search boxes or names
• type: indicates the type of input control – text, number, password
• name: give a name to the control, can be used at server side to get the value
• id: specify a unique id for an HTML element

<form action = "Script URL" method = "GET|POST">


First name: <input type = "text" name = "first_num“ id="first_num"
required="required" value="<?php echo $first_num; ?>" />
</form>

MA 518: Database Management Systems 23


Text Input Controls
• <input> tag is used for items that require only one line of user input,
such as search boxes or names
• type: indicates the type of input control – text, number, password
• name: give a name to the control, can be used at server side to get the value
• id: specify a unique id for an HTML element
• required: Boolean attribute, field must be filled out before submitting form

<form action = "Script URL" method = "GET|POST">


First name: <input type = "text" name = "first_num“ id="first_num"
required="required" value="<?php echo $first_num; ?>" />
</form>

MA 518: Database Management Systems 24


Text Input Controls
• <input> tag is used for items that require only one line of user input,
such as search boxes or names
• type: indicates the type of input control – text, number, password
• name: give a name to the control, can be used at server side to get the value
• id: specify a unique id for an HTML element
• required: Boolean attribute, field must be filled out before submitting form
• value: used to provide an initial value to the control

<form action = "Script URL" method = "GET|POST">


First name: <input type = "text" name = "first_num“ id="first_num"
required="required" value="<?php echo $first_num; ?>" />
</form>

MA 518: Database Management Systems 25


HTML Form: A simple calculator
<form action="" method="post" id="numbers">
<div> <span style=margin-right:1.25em> Enter first number: </span> <input type="number"
name="first_num" id="first" required="required" value="<?php echo $first_num; ?>"> </div>

</form>

MA 518: Database Management Systems 26


HTML Form: A simple calculator
<form action="" method="post" id="numbers">
<div> <span style=margin-right:1.25em> Enter first number: </span> <input type="number"
name="first_num" id="first" required="required" value="<?php echo $first_num; ?>"> </div>
<div> Enter second number: <input type="number" name="second_num" required="required" value="">
</div>

</form>

MA 518: Database Management Systems 27


HTML Form: A simple calculator
<form action="" method="post" id="numbers">
<div> <span style=margin-right:1.25em> Enter first number: </span> <input type="number"
name="first_num" id="first" required="required" value="<?php echo $first_num; ?>"> </div>
<div> Enter second number: <input type="number" name="second_num" required="required" value="">
</div>

<select name ="operator">


<option value = "Add" selected>Add</option>
<option value = "Subtract">Subtract</option>
<option value = "Multiply">Multiply</option>
<option value = "Divide">Divide</option>
</select>

</form>

MA 518: Database Management Systems 28


HTML Form: A simple calculator
<form action="" method="post" id="numbers">
<div> <span style=margin-right:1.25em> Enter first number: </span> <input type="number"
name="first_num" id="first" required="required" value="<?php echo $first_num; ?>"> </div>
<div> Enter second number: <input type="number" name="second_num" required="required" value="">
</div>

<select name ="operator">


<option value = "Add" selected>Add</option>
<option value = "Subtract">Subtract</option>
<option value = "Multiply">Multiply</option>
<option value = "Divide">Divide</option>
</select>

<p> <span text-align:center > <input type="submit" value="Compute"> </span> </p>


</form>

MA 518: Database Management Systems 29


MA 518: Database Management Systems 30
Processing POST data
…………..
</form>
<?php
if (isset($_POST['first_num']) && isset($_POST['second_num']) && isset($_POST['operator'])) {
$first_num=$_POST['first_num'];
$second_num=$_POST['second_num'];
$operator=$_POST['operator']; $result = '‘”;

}
?>

MA 518: Database Management Systems 31


Processing POST data
…………..
</form>
<?php
if (isset($_POST['first_num']) && isset($_POST['second_num']) && isset($_POST['operator'])) {
$first_num=$_POST['first_num'];
$second_num=$_POST['second_num'];
$operator=$_POST['operator']; $result = '‘”;
if (is_numeric($first_num) && is_numeric($second_num)) {

}
?>
MA 518: Database Management Systems 32
Processing POST data
…………..
</form>
<?php
if (isset($_POST['first_num']) && isset($_POST['second_num']) && isset($_POST['operator'])) {
$first_num=$_POST['first_num'];
$second_num=$_POST['second_num'];
$operator=$_POST['operator']; $result = '‘”;
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {

} }

}
?>
MA 518: Database Management Systems 33
Processing POST data
…………..
</form>
<?php
if (isset($_POST['first_num']) && isset($_POST['second_num']) && isset($_POST['operator'])) {
$first_num=$_POST['first_num'];
$second_num=$_POST['second_num'];
$operator=$_POST['operator']; $result = '‘”;
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;

} }
echo "Result:<input type='text' value=$result />";
}
?> MA 518: Database Management Systems 34
Processing POST data
…………..
</form>
<?php
if (isset($_POST['first_num']) && isset($_POST['second_num']) && isset($_POST['operator'])) {
$first_num=$_POST['first_num'];
$second_num=$_POST['second_num'];
$operator=$_POST['operator']; $result = '‘”;
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
……………………
} }
echo "Result:<input type='text' value=$result />";
}
?> MA 518: Database Management Systems 35
Thank You

MA 518: Database Management Systems 36

You might also like