PHP | Working with multiple forms

PHP | Working with multiple forms

A web page having multiple forms:

A web page having multiple forms can be processed in two types:

1. Posting each form to different PHP script file for processing

Multiple functionalities can be provided on a single web page by providing multiple forms on a web page having different functionality.

Each form on this web page will be given a separate name that will uniquely identify the form on a web page with multiple forms.

Data from each form should be given to separate PHP script file for processing by specifying PHP script filename in the action attribute of the forms.

Each PHP Script should be written in such a fashion that will handle all the data coming from that form.

A disadvantage of this method is that we have to write a separate PHP script for each form, which creates extra files for handing

Example:
<!DOCTYPE html>
<html>
<head>
 <title>cwipedia.in</title>
</head>
<body>
<form name="f1" method="POST" action="P1.php">
 RollNo:<input type="text" name="rno">
 <input type="submit" name="submit1">
</form>
<form name="f2" method="post" action="P2.php">
 Enrollment ID:<input type="text" name="eno">
 <input type="submit" name="submit2">
</form>
</body>
</html>
Output:


P1.php

<?php 
if(isset($_POST['submit1']))
{
 echo "Rollno: ",$_POST['rno'];
}
?>
P2.php


<?php 
if(isset($_POST['submit2']))
{
 echo "Enno: ",$_POST['eno'];
}
?>
Output:

2.Posting all form to single PHP script file for processing

Multiple functionalities can be provided on a single web page by providing multiple forms on a web page having different functionality.

Each form on this web page will be given a separate name that will uniquely identify the form on a web page with multiple forms.

Data from each form should be given to a single PHP script file for processing by specifying PHP script filename in the action attribute of the forms.

Each PHP Script should be written in such a fashion that it will handle all the data coming from multiple forms.

Data from multiple forms can be identified by it submit button and the processing each form will be written with help of if, else, and else if conditional statements.

The advantage of this method is that we have to write a single PHP script for processing of all forms, which saves time in the creation and handling of extra files.

Example:
<!DOCTYPE html>
<html>
<head>
 <title>cwipedia.in</title>
</head>
<body>
<form name="f1" method="POST" action="PP.php">
 RollNo:<input type="text" name="rno">
 <input type="submit" name="submit1">
</form>
<form name="f2" method="post" action="PP.php">
 Enrollment ID:<input type="text" name="eno">
 <input type="submit" name="submit2">
</form>
</body>
</html>
Output:


PP.php

<?php 
if(isset($_POST['submit1']))
{
 echo "Rollno: ",$_POST['rno'];
}
else if(isset($_POST['submit2']))
{
 echo "Enno: ",$_POST['eno'];
}


 ?>

Output:




Comments