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
Answer from cristi _b on Stack OverflowYou 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
Well you can have each form go to to a different page. (which is preferable)
Or have a different value for the a certain input and base posts on that:
switch($_POST['submit']) {
case 'login':
//...
break;
case 'register':
//...
break;
}
php - Two forms in one page - Stack Overflow
Need help with multiple forms on one page, here
Multiple Forms on One Page - PHP - SitePoint Forums | Web Development & Design Community
html - How to handle multiple php forms using one php page - Stack Overflow
You need to name your forms and handle them separately. You can move your form building code in a MailFormType class and create 2 named forms:
$form1 = $this->get('form.factory')
->createNamedBuilder('form1', MailFormType::class)
->getForm();
$form2 = $this->get('form.factory')
->createNamedBuilder('form2', MailFormType::class)
->getForm();
if ($request->request->has('form1') {
// handle form1
}
if ($request->request->has('form2') {
// handle form2
}
Indeed as Mihai Aurelian said you need to name the two forms in order to handle them correctly. As you can see from the posted html code, currently you have two forms containing inputs with the exactly same names. That's why it seems that both forms are submitted with exactly the same data. Since you are building your forms inside a controller and not in a separate Form Type class you should have something like this:
use Symfony\Component\Form\Extension\Core\Type\FormType;
public function yourAction(Request $request, FormFactoryInterface $formFactory) {
$defaultData = array('message' => 'Mail');
$form2 = $formFactory->createNamedBuilder('form2', FormType::class, $defaultData)
->add('content', TextareaType::class)
->getForm();
$defaultData2 = array('message2' => 'Mail2');
$form3 = $formFactory->createNamedBuilder('form3', FormType::class, $defaultData2)
->add('content', TextareaType::class)
->getForm();
$form2->handleRequest($request);
if ($form2->isSubmitted() && $form2->isValid()) {
// handle form2
}
$form3->handleRequest($request);
if ($form3->isSubmitted() && $form3->isValid()) {
// handle form3
}
}
EDIT: I fixed the missing $formFactory service from the action. But you should get FormFactory service through autowiring by type-hinting $formFactory argument in your action, instead of getting it directly from service container using $this->get('form.factory'). You should avoid getting services inside a controller this way, prefer autowiring whenever possible.
https://pastebin.com/qxigq13B
Apologies if my wording seems kind of vague here, but here goes:
For each row of mySql data that is returned, I want to make the row data its own form, include either a select menu (or a number input) and push them (both row data and input value) into an array on another file. The issue is, if I use $_GET or $_POST, the row items won't come through, and if I were to use $_SESSION, it would only take row data from the last form without including the value of the number/select input. Included is a relevant code snippet, with the number or select fields omitted.
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().
You could set different values for button submit for each form. Or you could use a input tag hidden to set type of form. Example:
Form1 : <input type="hidden" name="type" value="form1">
Form2 : <input type="hidden" name="type" value="form2">
And in php form
if($_POST["type"]=="form1")
{
//code here
}else if($_POST["type"]=="form2"){
//code here
}
Only the form which contains your submit-button should be triggered. Are you sure you closed both forms properly in the HTML?
Can you post a link to the page or paste all the HTML?
If your code is set like...
if(!empty($_POST)) {}
then it'll seem like both forms are submitted, but really you can only access data from the form that was actually submitted. If you put a name on the two submit buttons you can test for $_POST['submitButtonName'].
You can use this page to see the differences pretty easily.
http://pastie.org/pastes/2290937/text
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
}
}
create separate form_process script then include in form pages.
if(!empty($_POST)){
include 'form_process.php';
}
form_process.php should contain only class/function without echo or print out.
alternately you may set action url to the same page then redirect back to proper page.
<form id="add-profile-form" action="form_controller.php" method="post">
<input type="hidden" name="act" value="adding"/>
<!-- form 1. -->
</form>
<form id="edit-profile-form" action="form_controller.php">
<input type="hidden" name="act" value="editing"/>
<!-- form 2. -->
</form>
form_controller.php
if(isset($_POST['act']){
if($_POST['act'] == 'adding'){
//process form1
}else if($_POST['act'] == 'editing'){
//process form2
}
header("Location: success.php");
}
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters. If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you. Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
There are 2 approaches I could think of
if($POST['form_name'] == 'form1')
{
//set my session variables
}
else //for the second form
{
//do something else
//then session is intact
}
I used to put an hidden field in my form to store the name of the form. Some use the name of the submit button.
The other approach is repopulate the other form which am still thinking of how to do it without using one button for the two form
<form name="form1" action="POST" action="xxx">
<input type="text" values="<?php print($_POST['firstform_val_1']) ?>" name="val_1" />
</form>
<form name="form2" action="POST" action="xxx">
//these hidden fields preserve the formal values and make sure they are reposted in this second form
<input type="hidden" name="firstform_val_1" value="<?php print($_POST['firstform_val_1']) ?>" />
<input type="hidden" name="firstform_val_2" value="<?php print($_POST['firstform_val_2']) ?>" />
<input type="text" name="secondform_val1" />
<input type="submit" name="form2" />
</form>
That is the idea. I just got this from upstair, not tested. In the first form, the previous value is re-populated by the values duplicated in the hidden fields of the second form
Good Luck
If the form is being generated by a php page, you can use the following script to auto-populate any fields that were previously submitted.
Short tags enabled;
<input type="text" name="firstName" value="<?=isset($_POST['firstName'])?$_POST['firstName']:'';?>" ></input>
No Short tags;
<input type='text' name="firstName" value="
<?php
$value=isset($_POST['firstName'])?$_POST['firstName']:'';
echo($value);
?>
"></input>
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.
The action value is encoded in the query string.
PHP parses the query string into the $_GET superglobal. Only data from the request body will be parsed into the $_POST superglobal.
Use $_GET['action'] or move the data from the query string to an input.