0

Currently I'm working on a textbook ordering html form with PHP

User shown a list of textbook and theres a Quantity dropdown menu for each textbook (they can select to order 1 quantity or 2,3 or 4)

Each textbook have a unqiue textbookId number.

How can I create a html form which can determine which textbook they order and the quantity they order?

I usually solve this kind of problem by creating multiple html form (each form contain hidden field of textbookId) but the problem is that's there will be multiple submit button.

But I only one submit button for the whole textbook list.

Can someone please help me :) thanks so much in advance

If possible can you show me how to process the data in php as well.

Cheers

Here is my current html form code:

<div class="tables">
 <form action="process-order" id="login-form" method="get" name="login-form"></form>
 <table class="orders">
 <tr>
 <th>textbookId</th>
 <th>Subject</th>
 <th>Title</th>
 <th>Price</th>
 <th>Quantity</th>
 </tr>
 <tr>
 <td>1</td>
 <td>English</td>
 <td>English ABC</td>
 <td>33.00</td>
 <td>
 <div align="center">
 <select name="mydropdown">
 <option value="0">0</option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 </select>
 </div>
 </td>
 </tr>
 <tr>
 <td>2</td>
 <td>Math</td>
 <td>Math SignPost</td>
 <td>122.00</td>
 <td>
 <div align="center">
 <select name="mydropdown">
 <option value="0">0</option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 </select>
 </div>
 </td>
 </tr>
 </table>
 <input type="submit" name="submit" id="submit" value="Next">
 </form>
</div>
antyrat
27.8k9 gold badges78 silver badges77 bronze badges
asked Sep 20, 2011 at 15:21

3 Answers 3

1

One possibility is to encode the book ID into the submitted form element e.g. <select name="book_1"> for the book 1.

<tr>
 <td>1</td>
 <td>English</td>
 <td>
 <div align="center">
 <select name="book_1">
 <option value="0">0</option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 </select>
 </div>
 </td>
</tr>
<tr>
 <td>2</td>
 <td>Math</td>
 <td>
 <div align="center">
 <select name="book_2">
 <option value="0">0</option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 </select>
 </div>
 </td>
</tr>

PHP processes the books as follows:

<?php
$books = array();
if ($_GET) {
 foreach ($_GET as $key => $value) {
 $keys = split("_", $key);
 if (count($keys) == 2 && $keys[0] == "book") {
 $id = $keys[1];
 $books[$id] = $value;
 }
 }
}
// echo "Ordered: <br />";
// foreach ($books as $id => $value) {
// echo "$id : $value <br />";
// }
?> 
answered Sep 20, 2011 at 16:28
Sign up to request clarification or add additional context in comments.

Comments

1

I suposse you already have a table in your data base to store the data from the form, right?

Then I will output here only the php side, all right?

action.php

<?php
if ($_POST) {
$mydropdown = $_POST['mydropdown'];
$mydropdown2 = $_POST['mydropdown2'];
$response = mysql_query("INSERT INTO your_table ('id','mydropdown','mydropdown2') values (NULL,'$mydropdown','$mydropdown2') ");
if (empty($response)) { die("Error in your query"); }
}
?>

Observation: Your have the name, prices, etc of your books, if you want to store this information you need to create input fields, otherwise this information will not be stored in your data base.

Something like this in your HTML side. <td>English ABC</td> -> <input type="text" value="" name="name_book" /> do the same as before mentioned to your input fields.

answered Sep 20, 2011 at 15:43

Comments

0

Have you thought about using javascript to resolve this? specifically jQuery? You can call a function to iterate through, something like:

<script>
 function submitChanges() {
 $('.mydropdown').each(function(){
 ddlId = $(this).attr('id');
 id = ddlId.split('_');
 ddlVal = $(this).val();
 $.ajax({
 type:'POST',
 url: <post_url>,
 data: 'id='+id[1]+'val='+ddlVal,
 success: function (){}
 });
 });
 }
</script>

So the function above will POST the selections to the given page, benefit would be that you can have as many books as you want.

When you generate your HTML, make sure each select has 'class' and 'id' defined:

<select class="mydropdown" id="mydropdown_{textbookid}" name="mydropdown">
 <option value="0">0</option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
</select>

The textbookid would be added via PHP

In the post PHP would contain

if ($_POST) {
 $id= $_POST['id'];
 $val= $_POST['val'];
 $query = "INSERT INTO purchase (textbookid, quantity) VALUES ({$id},{$val})";
}
answered Sep 20, 2011 at 16:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.