karan Posted June 30, 2017 Share Posted June 30, 2017 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! Quote Link to comment Share on other sites More sharing options...
requinix Posted June 30, 2017 Share Posted June 30, 2017 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]; Quote Link to comment Share on other sites More sharing options...
karan Posted June 30, 2017 Author Share Posted June 30, 2017 I don't understand. The table is initially empty. So where will the form be placed? inside the function addRow() or outside, before the table definition? And the Submit button? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 30, 2017 Share Posted June 30, 2017 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. Quote Link to comment Share on other sites More sharing options...
karan Posted June 30, 2017 Author Share Posted June 30, 2017 ok and one more thing. How do I display the values? foreach ($_POST['code'] as $c){ echo $c; } like that? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 30, 2017 Share Posted June 30, 2017 That code will display values, sure. Quote Link to comment Share on other sites More sharing options...
karan Posted June 30, 2017 Author Share Posted June 30, 2017 Got it, thanks a ton bro. I have another thing though. My sessions are not working properly. I'll post the question too. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
× Pasted as rich text. Restore formatting
Only 75 emoji are allowed.
× Your link has been automatically embedded. Display as a link instead
× Your previous content has been restored. Clear editor
× You cannot paste images directly. Upload or insert images from URL.