Trouble with turning 1 order button for each result into only one order button

I am busy creating an order system.

I am having issues trying to create a form that requires only one order button.

Currently the form is for each product result. The user has the ability to enter in a quantity desired of a certain product, then can order the product based on the quantity chosen. The item id and quantity chosen are then added to sessions.

The problem with this, is that if there are 5 items, there are 5 order buttons.

I am trying to simplify this, where for 5 products, there will be only one order button controlling the entire selection of products.

Here is the code relating to this:

//Now we search for our search term, in the field the user specified 

$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE '%" 
   . implode("%' AND upper(`desc`) LIKE '%", $keywords_array) 
   . "%' ORDER BY `desc`";

$data = mysql_query($dataQuery) or die(mysql_error());

$tempVar = 0;
//And we display the results 
while ($result = mysql_fetch_array($data)) {
$prId = $result['id'];
$prRefCode = $result['refCode'];
$prDesc = $result['desc'];
$prPack = $result['pack'];
$prMeasure = $result['measure'];
$prQuantity = $result['quantity'];
$prDeptCode = $result['deptCode'];
$prTaxable = $result['taxable'];
$prPrice1 = $result['price1'];
$prPrice2 = $result['price2'];
$prCrdCode = $result['crdCode'];
$prCost1 = $result['cost1'];
$prCost2 = $result['cost2'];

if ($tempVar == 0) {
$pageContent .= '
<p><u>All prices are inclusive of VAT</u></p>
<table class="searchResults" border="1" align="center" width="90%">
<thead>
    <tr>
        <th>Stock Code</th>
        <th>Description</th>
        <th>Packsize</th>
        <th>Price</th>
        <th>In-Stock?</th>
        <th>Quantity</th>
        <th>Submit</th>
    </tr>
</thead>
<tbody>
';
}

    $pageContent .= '
<tr>
    <td>' . $prId . '</td>
    <td>' . $prDesc . '</td>
    <td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td>
    <td>R' . $prPrice1 . '</td>
';
    if (empty($prQuantity)) {
        $pageContent .= '
<td>No</td>
';
    } else {
        $pageContent .= '
<td>Yes</td>
';
    }
    $pageContent .= '
    <form id="validated" action="" method="post">
    <td>            
            <div>
                <input type="text" 
onkeydown="return ( event.ctrlKey || event.altKey 
                || (47<event.keyCode && event.keyCode<58 && event.shiftKey==false) 
                || (95<event.keyCode && event.keyCode<106)
                || (event.keyCode==8) || (event.keyCode==9) 
                || (event.keyCode>34 && event.keyCode<40) 
                || (event.keyCode==46) )" 
                name="quantity" size ="2" 
                value ="1" 
                style="background: #F4F4F4;
                       font-family: Monaco, monospace;" />
            </div>            
    </td>
    <td>
            <div>
                <input type="hidden" name="id" value="' . $prId . '" />
                <input type="submit" name="action" value="Order" />
            </div>            
    </td>
    </form>     
</tr>
';
$tempVar ++;
}



//This counts the number of results - and if there wasn't any it gives them a little message explaining that 
$anymatches = mysql_num_rows($data);
if ($anymatches == 0) {
    $pageContent .= '
<p>Sorry, but we can not find an entry to match your query</p>
<!-- end .aliLeft --></div>
';
}

if ($anymatches > 0 and count($tempVar) == count($anymatches)) {
    $pageContent .= '
</tbody>
</table>
<!-- end .aliLeft --></div>
';
}

And here is the code controlling the instance of a user clicking on the order button:

if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}

if (!isset($_SESSION['quantity']))
{
$_SESSION['quantity'] = array();
}

if (isset($_POST['action']) and $_POST['action'] == 'Order' and $_POST['quantity'] > 0)
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][$_POST['id']] = $_POST['id'];
$_SESSION['quantity'][$_POST['id']] = $_POST['quantity'];
header('Location: .');
exit();
}

As you can see, when the user searches for a product, the mysql table is queried, if results are found, then they are returned.

In this process, the form is created for each result.

What I am trying to do, is extend this form to cover the entire set of results, then have only one input for “Order”, so essentially, the user can enter quantities on various products, then have those products and quantities added to their order.

Does anyone have any advice on how i would go about this task?

I would appreciate any input what so ever, thank you!

Essentially, to keep everything in one form you need to associate the id of the item with the quantity. For that use an array eg for item id number 99:


<input type = text name="quantity[99]" size ="2" value ="1"/>

Create yourself a little test form using hardcoded values like that above, submit it back to itself and inspect carefully the result of


<?php
// first line of your file
if( isset($_POST) ){

var_dump($_POST);
}
?>

// create a simple html form here
<form method=post action = ''>
<input type = text name="quantity[99]" size ="2" value ="1"/>
<input type = text name="quantity[17]" size ="2" value ="1"/>
<input type=submit>
</form>