This question already has answers here:
How to submit 2 forms in one page with a single submit button
(4 answers)
Closed 7 years ago.
I have two forms that look like this:
<h2>First form</h2>
<form id="first-form">
<table>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
</table>
</form>
<!-- some more HTML -->
<h2>Second form</h2>
<form id="second-form">
<label>
<input type="search" name="sentence" value="i like the bananas">
</label>
<label>
<input type="text" name="parse-as" value="S">
</label>
<label>
<select name="include-features">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<label>
<select name="show-earley">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<button type="submit">Parse</button>
</form>
Note that the first form can contain more rows; the user can add more rows to the form. As you can see, there's only one submit button. That's because both forms have to be submitted together. In jQuery I'd do something like this:
$("#second-form").submit(function(event) {
$("first-form").submit();
event.preventDefault();
});
As the front-end developer of the team, I need to pass down the values of both forms to the back-end. Unfortunately I'm at loss how to do this. I figured I could do something like this:
$("#second-form").on("submit", function(event) {
$("#first-form").submit();
$.post("php/do.php", $("#second-form").serialize());
event.preventDefault();
});
$("#first-form").on("submit", function(event) {
$.post("php/do.php", $("#first-form").serializeArray());
event.preventDefault();
});
.serializeArray() seems necessary because of the array-like values in the HTML for #first-form. In the second form, however, we don't need an array. The issue I'm having now is, what code do I need in do.php that can handle the input. How do you access the data, i.e. the serialized forms in PHP?
I tried a number of things such as
<?php
$data = parse_str($_POST["right[]"]);
var_dump($data);
$date1 = parse_str($_POST["right[][0]"]);
var_dump($data);
$data2 = parse_str($_POST["right[][1]"]);
var_dump($data);
?>
But all of these return an Undefined index error, which seems to mean that my "selector" is wrong. How would I go about selecting the array from the first form, and the values of the second form in PHP? And is my jQuery method of submitting first-form together with the second correct?
Why don't you do this :
<h2>First form</h2>
<form id="first-form">
<table>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
</table>
<!-- some more HTML -->
<h2>Second form</h2>
<label>
<input type="search" name="sentence" value="i like the bananas">
</label>
<label>
<input type="text" name="parse-as" value="S">
</label>
<label>
<select name="include-features">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<label>
<select name="show-earley">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<button type="submit">Parse</button>
</form>
Maybe it's not the cleanest way to do it, but it's the easier. Just get the results with :
<?php
$data = parse_str($_POST["right[]"]);
var_dump($data);
$date1 = parse_str($_POST["right[][0]"]);
var_dump($data1);
$data2 = parse_str($_POST["right[][1]"]);
var_dump($data2);
?>
Related
I have a table with six rows and i have assigned same names to the values to each row , how can i save the six row values into to the database using php in codeigniter
<td>
<input type="text" name="compscheme" id="compscheme" class="form-control m-b-10">
</td>
<td>
<input type="text" name="compbrands" id="compbrands" class="form-control m-b-10">
</td>
<td>
<input type="text" name="companyname" id="companyname" class="form-control m-b-10">
</td>
<td>
<input type="text" name="cmpyscheme" id="cmpyscheme" class="form-control m-b-10">
</td>
</tr>
<tr>
<td>
<input type="text" name="compscheme" id="compscheme" class="form-control m-b-10">
</td>
<td>
<input type="text" name="compbrands" id="compbrands" class="form-control m-b-10">
</td>
<td>
<input type="text" name="companyname" id="companyname" class="form-control m-b-10">
</td>
<td>
<input type="text" name="cmpyscheme" id="cmpyscheme" class="form-control m-b-10">
</td>
in this way i have six rows. should i use multiple array tag or any other way for inserting
you should use array input name like- companyname[] -
<!--
<td>
<input type="text" name="compscheme[]" id="compscheme" class="form-control m-b-10">
</td>
<td>
<input type="text" name="compbrands[]" id="compbrands" class="form-control m-b-10">
</td>
.......
.......
.......
-->
<?php
if(($this->input->server('REQUEST_METHOD')=='POST')){
for ($i=0; $i <count($_POST['cmpyscheme']); $i++) {
$data=array(
'table_column_name'=>$_POST['cmpyscheme'][$i],
);
// for 6 rows
if($i==5){
echo $_POST['cmpyscheme'][$i];
}
}
}
?>
You can get directly value of six row.
$cmpyscheme=$_POST['cmpyscheme'][5];
$compbrands=$_POST['compbrands'][5];
$companyname=$_POST['companyname'][5];
$cmpyscheme=$_POST['cmpyscheme'][5];
Here's my table in database :
Then this is my code :
<tr>
<td width="50">
<div align="right">Username</div>
</td>
<td width="100">
<input name="username" type="text" />
</td>
</tr>
<tr>
<td>
<div align="right">Password</div>
</td>
<td>
<input name="password" type="password" />
</td>
</tr>
<tr>
<td>
<select class="w3-select" name="option">
<option value="" disabled selected>User Type
</option>
<option value="1">Admin
</option>
<option value="2">Doctor
</option>
</select>
</td>
</tr>
<tr>
<td>
<div align="right"></div>
</td>
<td>
<input name="" type="submit" value="Login" />
</td>
</tr>
The problem is how can I call admin.php when I click admin in the user type?
Or should i remove usertype in html/php and should base in database? thank you so much
You need a <form> where you pass the data as $_GET or $_POST variables.
Something like this:
<form action="admin.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Enter restricted area">
</form>
Since this code uses method="post", the input data will then be stored in the global $_POST variable. This means that in admin.php you can access them as $_POST["username"] and $_POST["password"].
I suggest you to read the tutorial: PHP forms tutorial
PS: For better security, you should not store the password in the database. Instead, encrypt it with a hash algorithm and store the encrypted version: password_verify manual
Let's say that I have an HTML structure that looks like this. Each left-context-field and right-context-field are related and in the backend it should be possible to link left to its right colleague.
<table>
<tbody>
<tr>
<td>
<input type="text" class="left-context-field" value="First left value">
</td>
<td>
<input type="text" class="right-context-field" value="First right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Second left value">
</td>
<td>
<input type="text" class="right-context-field" value="Second right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Third left value">
</td>
<td>
<input type="text" class="right-context-field" value="Third right value">
</td>
</tr>
<tr>
<td>
<input type="text" class="left-context-field" value="Fourth left value">
</td>
<td>
<input type="text" class="right-context-field" value="Fourth right value">
</td>
</tr>
</tbody>
</table>
and a user can add as many additional rows as they please. How would I go about posting the values to a PHP file?
Should I wrap all rows in one single huge form, and then call .serializeArray()? Or one form per row?
From a data-oriented approach, what's the best way to deal with this? I'll be sending the data to the back-end, but I want to make the data ass easily accessible for the the backend developer as possible.
I've Edited meda's Answer, but he is not approving that Edit, So I'm posting it here again.
In HTML forms the way you send arrays is using brackets.
For example
name="values[]"
like this.
<form name = 'vals_form' action = 'php_page.php' method = 'post'>
<table>
... <!-- Your all code -->
<tr>
<td>
<input type="text" class="left-context-field" value="First left value" name="values[]">
</td>
<td>
<input type="text" class="right-context-field" value="First right value" name="values[]">
</td>
</tr>
...
<input type = 'submit' value = 'Post Values' />
</table>
</form> <!--and close the form tag-->
In PHP you get the array back
$values = $_POST['values'];
In HTML forms the way you send arrays is using brackets.
For example
name="values[]"
like this.
<form name = 'vals_form' action = 'php_page.php' method = 'post'>
<table>
... <!-- Your all code -->
<tr>
<td>
<input type="text" class="left-context-field" value="First left value" name="values[]">
</td>
<td>
<input type="text" class="right-context-field" value="First right value" name="values[]">
</td>
</tr>
...
<input type = 'submit' value = 'Post Values' />
</table>
</form> <!--and close the form tag-->
In PHP you get the array back
$values = $_POST['values'];
I'm trying to pass multiple values from same parameters of a form using POST, but can't figure out how to proceed. I've used bootstrap css such that I can add multiple products. And I want to process the data of multiple orders by passing the values using POST method.
On clicking 'Add another' link, additional set of data field appear which enables recording of multiple transactions of a same user.
The code is as follows:
<div class="col-xs-5 col-lg-offset-3">
<form action="billingProceed.php" method="post" role="form">
<table id="itemElement">
<tr>
<td>
<select class="form-control">
<option class="form-control"> Customer Name</option>
<option class="form-control"> Customer ID</option>
</select>
</td>
<td><input type="text" name="<?php echo $data["name"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td>
<select class="form-control">
<option class="form-control"> Item Name</option>
<option class="form-control"> Item ID</option>
</select>
</td>
<td ><input type="text" name="<?php echo $data["item"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Quantity
</td>
<td><input type="text" name="<?php echo $data["quantity"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Price
</td>
<td><input type="text" name="<?php echo $data["price"]; ?>" class="form-control" /></td>
</tr>
<tr>
<td style="float:right;">Discount
</td>
<td><input type="text" name="<?php echo $data["discount"]; ?>" class="form-control" /></td>
</tr>
</table>
<input type="submit" value="Proceed" class="btn btn-primary" />
<p style="float:right;">Add another</p>
You can use an array name.
Example:
<input name="data['name'][1]">
<input name="data['name'][2]">
Firstly you should be aware of the name array, for input names.
HTML Example:
<form>
<a id="add_another" href="#">Add Another</a>
<table>
<tr class="product_item">
<td>
<input type="text" name="product[1][name]" value=""/>
</td>
<td>
<input type="text" name="product[1][item]" value=""/>
</td>
</tr>
<tr id="dummy">
<td>
<input type="text" name="product[0][name]" value=""/>
</td>
<td>
<input type="text" name="product[0][item]" value=""/>
</td>
</tr>
</table>
</form>
On POST, in your PHP script, you will access them as follows:
foreach($_POST['product'] as $product)
{
$name = $product['name'];
$item = $product['item'];
}
Shoot for the JS
//You'll always start with one product row.
var productCount = 1;
$('#add_another').click(function() {
var dummyproduct = $('#dummy').clone();
//Increment Product count
productCount += 1;
//Rename all inputs
dummyproduct.find('input[name^="product[0]"]').each(function(){
$(this).attr('name',$(this).attr('name').replace('product[0]','product['+productCount +']'));
});
//Remove Id from cloned dummy
dummyproduct.removeAttr('id').addClass('product_item');
//Insert row before Dummy
dummyproduct.insertBefore('#dummy');
});
I have gone through a bunch of question posts, and found one person with the same issue as me, but the answer he/she received I did not find to help at all.
I created a form which uses checkboxes. Once submitted my entire form is processed beautifully and I do receive the mail, but when it comes to the checkboxes it only displays "Array" in the email in stead of the checked checkbox values....
What am I doing wrong?
HTML Form Code: as requested, the full form
<form name="busquoteform" method="post" action="FormToEmail.php">
<fieldset>
<legend>Contact Information</legend>
<table width="100%">
<tr>
<td width="40%">
<label><strong>Name *:</strong></label><br/>
<input name="name" type="text" id="name" value="" />
</td>
<td width="10%"> </td>
<td width="40%">
<label><strong>Lastname *:</strong></label><br />
<input name="lname" type="text" id="lname" value="" />
</td>
</tr>
</table>
<table width="100%">
<tr>
<td width="25%">
<label><strong>Contact Number:</strong></label><br/>
<input name="contactno" type="text" id="contactno" value="" />
</td>
<td width="25%">
<label><strong>Mobile Number * </strong></label><br/>
<input name="mobno" type="text" id="mobno" value="" />
</td>
<td width="40%">
<label><strong>Email *:</strong></label><br/>
<input name="email" type="text" id="email" value="" />
</td>
</tr>
</table>
</fieldset>
<br/>
<fieldset>
<legend>Company Information</legend>
<table width="100%">
<tr>
<td width="40%">
<label><strong>Company Name *:</strong></label><br/>
<input name="compname" type="text" id="compname" value="" />
</td>
<td width="10%">
<label><strong>Position Held *:</strong></label><br />
<input name="position" type="text" id="position" value="" />
</td>
<td width="40%">
</td>
</tr>
</table>
<table width="100%">
<tr>
<td width="16%">
<label><strong>Company Address*:</strong></label><br/><br/><br/><br/>
</td>
<td width="2%">
</td>
<td>
<input name="street" type="text" id="street" value="Street" size="30" /><br/>
<input name="suburb" type="text" id="suburb" value="Suburb" size="30" /><br/>
<input name="city" type="text" id="city" value="City" size="30" /><br/>
<input name="code" type="text" id="code" value="Postal Code" size="10" /><br/>
</td>
</tr>
</table>
</fieldset>
<br/>
<fieldset>
<legend>Project Information</legend>
<table>
<tr>
<td>
<label><strong>Service Type/s*:</strong></label><br/>
Please select all applicable types.
</td>
</tr>
<tr>
<td>
Graphic Design <input name="serviceType[]" id="design" type="checkbox" value="Graphic Design" />
Web Development <input name="serviceType[]" id="webdev" type="checkbox" value="Web Development" />
Application Development <input name="serviceType[]" id="appdev" type="checkbox" value="App Development" />
Embroidery <input name="serviceType[]" id="embroidery" type="checkbox" value="Embroidery" />
Engraving <input name="serviceType[]" id="engrave" type="checkbox" value="Engraving" /><br/><br/>
</td>
</tr>
<tr>
<td>
<label><strong>Please supply a detailed description of your requirements*:</strong></label><br/>
<textarea name="projectDes" cols="60" rows="10" id="projectDes"></textarea>
<br/><br/>
<input name="quoteBus" type="submit" class="ZD-button" value="Send Request"/>
</td>
</tr>
</table>
</fieldset>
</form>
and the php processing code:
$mailBody = "Name : ".$_REQUEST['name']. " ".$_REQUEST['lname'].
" <br/>Email : ".$_REQUEST['email'].
" <br/>Contact No : ".$_REQUEST['contactno']. " Mobile No: ".$_REQUEST['mobno'].
"<br/><br/>Company Name : ".$_REQUEST['compname'].
" <br/>Postion Held : ".$_REQUEST['position'].
"<br/><br/>Company Address : <br/>".$_REQUEST['street']."<br/>".$_REQUEST['suburb']."<br/>".$_REQUEST['city']."<br/>".$_REQUEST['code'].
"<br/><br/> Service Type/s :" .(is_array($_REQUEST['serviceType'])?implode("\n", $_REQUEST['serviceType']):$_REQUEST['serviceType'])."<br />".
"<br/><br/>Details of Project : ".$_REQUEST['projectDes'];
I've also tried:
" Service Type/s :" .$serviceType = $_POST["serviceType"];$serviceType = implode(', ', $serviceType);"".
and also does not seem to work...
I got this code from a project my hubby did a while back - but he is not a php developer, he's into Java...
Help Please?
Update
I just tried your code and it works fine, and results what you want. So can you please show your <form> tag.
Code that I tested
<?php
echo (is_array($_REQUEST['serviceType']) ? implode("\n", $_REQUEST['serviceType']) : $_REQUEST['serviceType']);
?>
<form action="" method="post">
Graphic Design <input name="serviceType[]" id="design" type="checkbox" value="Graphic Design" />
Web Development <input name="serviceType[]" id="webdev" type="checkbox" value="Web Development" />
Application Development <input name="serviceType[]" id="appdev" type="checkbox" value="App Development" />
Embroidery <input name="serviceType[]" id="embroidery" type="checkbox" value="Embroidery" />
Engraving <input name="serviceType[]" id="engrave" type="checkbox" value="Engraving" /><br/><br/>
<input type="submit" />
</form>
Hm, that's strange.. It should show all the serviceType values seperated by \n, due to this line:
(is_array($_REQUEST['serviceType'])?implode("\n", $_REQUEST['serviceType']):$_REQUEST['serviceType'])
What if you change $_REQUEST to $_POST?
You can use foreach this way:
$serviceTypes = "";
if (is_array($_REQUEST['serviceType']))
{
foreach ($_REQUEST['serviceType'] as $serviceType)
{
$serviceTypes.= "$serviceType\n";
}
}
It's a possibility.