Multiple forms and one processing page
It's not completely unheard of to do this. Quite often, a different parameter is passed in the form element's action attribute like /submit.php?action=register
or /submit.php?action=activate
.
So, you have code like this:
if ($_GET['action'] == 'register') {
// Register user
} else if($_GET['action'] == 'activate' {
// Activate user
}
However, you could also just change the value of the submit button and have the action attribute the same for both forms:
if (isset($_POST['submit'])) {
if ($_POST['submit'] == 'register') {
// Register user
} else if($_POST['submit'] == 'activate') {
// Activate user
}
}
Using php to process multiple forms on the one page
If you want the action
to be read as POST, try putting it in a hidden input instead. One of your forms would look like this:
<form action="editmenusprocess.php" method="POST">
<legend><h3 style="margin-top: 20px;">Add Starter</h3></legend>
<div class="form-group">
<label for="exampleInputEmail1">Food:</label>
<input type="text" class="form-control form-control-lg" name="msfood" placeholder="Title">
<input type="hidden" name="action" value="starter">
</div>
<!-- THE REST OF THE INPUT FIELDS BELOW -->
Do the rest to your other forms.
Then, in your editmenusprocess.php:
$action = (!empty($_POST["action"]))?$_POST["action"]:null;
if($action == 'starter'){
$stmt = $link->prepare("INSERT INTO 2043menustarter (food, descript, price) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $_POST["msfood"], $_POST["msdescript"], $_POST["msprice"]);
$stmt->execute();
$stmt->close();
}
/*** THEN THE REST OF YOUR OTHER CONDITIONS BELOW ***/
In the comment section of your post, people have been suggesting that you at least use prepared statement. You can read more about it here.
How to place two forms on the same page?
You could make two forms with 2 different actions
<form action="login.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
<br />
<form action="register.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Register">
</form>
Or do this
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="login">
<input type="submit" value="Login">
</form>
<br />
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="register">
<input type="submit" value="Register">
</form>
Then you PHP file would work as a switch($_POST['action']) ... furthermore, they can't click on both links at the same time or make a simultaneous request, each submit is a separate request.
Your PHP would then go on with the switch logic or have different php files doing a login procedure then a registration procedure
How to handle multiple php forms using one php page
Consider using isset()
to check for a specific variable. It can be better then checking with empty()
.
<!DOCTYPE html>
<html>
<head>
<title>PHP demo</title>
</head>
<body>
<?php
if(isset($_POST['form1'])){
echo "<h1>student information</h1>\r\n";
echo "title is : $_POST['pullDownMenu']<br />\r\n";
echo "first name is : $_POST['firstname']<br />\r\n";
echo "lastname is : $_POST['lastname']\r\n";
}
if (isset($_POST['form2'])) {
echo "<p>Following details are saved to database</p>\r\n";
echo "reg No\t:\t$_POST['RegNO']<br />\r\n";
echo "NIC\t:\t$_POST['NIC']<br />\r\n";
echo "Tel No\t:\t$_POST['Telephone']<br />\r\n";
}
?>
</body>
</html>
If you have more forms, consider using switch()
instead of if()
.
web2py: multiple forms on one page
In your code, you create multiple forms in the for
loop, but after exiting the loop, you call form.accepts()
. At that point, the value of form
is the last form created in the loop, so only that form is processed.
Note, when a form is initially created, the form.accepts
(or the preferred form.process
) method adds the _formname
and _formkey
hidden fields to the form (these are used for CSRF protection). When that same method is called after form submission, it additionally handles form validation. So, given your workflow, you must process all the forms both at creation and submission. Maybe something like this:
products = db(db.product.group_id == productgroupnumber).select()
forms = []
for product in products:
quantity_name = 'quantity_%s' % product.id
form = FORM(TABLE(TR(TD(product.productname),
TD((product.purchasecost or 0)),
TD((product.monthlycost or 0)),
TD(INPUT(_type='number', _name=quantity_name)),
TD(INPUT(_type='submit', _value=T('Add to Offer')))
)
)
)
if form.process(formname=product.id, keepvalues=True).accepted:
offeritem = [product.id, form.vars[quantity_name],
product.purchasecost, product.monthlycost]
session.quotedproducts.append(offeritem)
response.flash = T("Item added to offer")
forms.append(form)
Related Topics
PHP How to Get the Base Domain/Url
How to Use Two Submit Buttons, and Differentiate Between Which One Was Used to Submit the Form
Required_If Laravel 5 Validation
Simple Comet Example Using PHP and Jquery
How to Use PHP in a JavaScript Function
Using Laravel Socialite to Login to Facebook
Select Within 20 Kilometers Based on Latitude/Longitude
Inserting Now() into Database with Codeigniter's Active Record
PHP Datetime Microseconds Always Returns 0
How to Install an Older Version of PHPunit Through Pear
How to Access a Different Controller from Inside a Controller Symfony2
PHP Dom Get Nodevalue HTML? (Without Stripping Tags)
How to Send Notification to Android from PHP