Jump to content

Insert multiple values in a form and submit with one button


karan

Recommended Posts

I have the following code. The problem is that I want the user to enter the 'code' and the 'quantity'. Then the 'code' is passed into a database using php and the values of 'name' and 'price' are retrieved and then the rest of the columns are populated with the retrieved values. The main problem is entering the data in the table and then submitting it using one button. The code is:

 

<!DOCTYPE HTML>
<html>
<body>
<script type = "text/javascript" >
function addRow()
{
var table = document.getElementById('order');
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = "<form action = 'm3.php' method = 'POST' id = 'form1'> <input type = 'text' name = 'code' /><input type = 'submit' name = 'submit' value = 'Submit'/></form>"; 
cell2.innerHTML = "";
cell3.innerHTML = "";
cell4.innerHTML = "<input type = 'text' name = 'qty' />";
cell5.innerHTML = "";
}
function delRow(r) 
{
document.getElementById('order').deleteRow(-1);
} 


</script>
<?php
echo $_POST['code']; //gives me the last code entered
?>
<p><strong> Order Details </strong></p>
<table id = "order" border = 1 border_collapse = "collapse" >
<tr>
<th> Item Code </th>
<th> Name </th>
<th> Price </th>
<th> Quantity </th>
<th> Total </th>
</tr>
<input type = "button" value = "Add Row" onclick = "addRow('order')" />
<input type = "button" value = "Delete Row" onclick = "delRow('order')" />
</body>
</html>

I am very new to php and javascript. Any help will be great. Thanks!

 

Link to comment
Share on other sites

1. Put a single

around the entire table, instead of having one in each row.

2. Name each field using array syntax: code[] and qty[].

3. $_POST["code"] and $_POST["qty"] will be arrays. You can get both values at once for each row like

foreach ($_POST["code"] as $key => $code) {
	$qty = $_POST["qty"][$key];
Link to comment
Share on other sites

Around the

. Opening before the opening
, closing after the closing
(which you seem to have forgotten). The point is that you have just the one
and all the form fields are somewhere inside of it.

 

Submit button can go wherever you want. Probably with the other buttons. It also needs to be within the

so actually put the closing
after the buttons.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.