(PHP) POST'ing values from a table with checkboxes?

Associate
Joined
2 Nov 2007
Posts
488
I have a question, that i was looking for some help for regarding a search stock page that i have been building:

As you can see from the code below, the search query outputs a table. The user first checks a row corresponding to an item they want (the RFQ column) and then one of the columns (called Quantity), in where the user can enter a quantity of the product they want. They then click a button and a request for quotation will be sent to the company (at the moment im just trying to echo out the selections made).

The query and output:
PHP:
                         while ($rows = mysql_fetch_array($query))
                        {
                                echo '<tr>';
                                echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>';
                                echo '<td>' . $rows['Part Number'] . '</td>';
                                echo '<td>' . $rows['Manufacturer'] . '</td>';
                                echo '<td>' . $rows['Stock'] . '</td>';
                                echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[]" tabindex="' . $tabindex++ .'" /></td>';
                                echo '<td><select name="Delivery" tabindex="' . $tabindex++ .'">
                                                <option value="Any">Any</option>
        <option value="Emergency">Emergency</option>
        <option value="Next Day">Next Day</option>
        <option value="2-3">2-3 Days</option>
        <option value="4-6">4-6 Days</option>
        <option value="7-10">7-10 Days</option>
        <option value="10+">10+ Days</option>
                                        </select></td>';
                                echo '</tr>';
                        }
                        echo '</table>';
                        echo $rows;
                        
                        // Send request for quotation
                        echo '<input type="submit" name ="SendRFQ" value="Request Quotation">';
                        echo '</form>';

Which is then displayed:
PHP:
// Check if parts have been selected for quotation already
        if (isset($_POST['SendRFQ']))
        {
                $RFQ = $_POST['RFQ'];
                $Quantity=$_POST['Quantity'];
                $NumberRFQ = count($RFQ);

                // List parts selected for quotation
                if ($NumberRFQ>0)
                {
                        echo 'RFQs chosen: '.$NumberRFQ.'<br /><br />';
                        echo 'You chose the following:<br /<br />';
                        
                        echo '<table border="1" align="center">';
                        echo '<tr><th>Part Number</th>';
                        echo "<th>Quantity?</th></tr>";

                        for ($i=0; $i<$NumberRFQ; $i++)
                        {
                                echo '<tr>';
                                echo '<td>' . $RFQ[$i] . '</td>';
                                echo '<td>' . $Quantity[$i] . '</td>';
                                echo '</tr>';
                                

                        }
                        echo "</table>";
                        echo '<br /><br />';
                }
                else
                {
                        echo 'No parts have been chosen for quotation<br /><br />';
                        echo '<a href="/search.php">Click here</a> to go back<br /><br />';
                }
        }

Which kinda works.

If i select say, the first three rows sequentially, the quantities are displayed correctly.
But say i select rows 1, 3 and 5 (and enter quantities as 1, 3 and 5 respectively), then row 1 shows quantity 1, row 2 shows nothing and row 3 shows quantity 3?!

Any ideas?

Cheers
 
Hi,

As the Qty[] Field is there all the time the $Quantity=$_POST['Quantity'] will be made up of the values of every Qty field comma delimited, unlike the RFQ which will only enter comma delimited values for the checked items.

the values for Qty would therefore (for 1,3,5 selected) be the 1st, 3rd and 5th array items and not 1,2,3 as per the loop code.

You could name each Qty field using the RFQ ID as well Quantity1, Quantity2, Quantity3 etc and then in your loop just get the Qty Values?

$_POST['Quantity' . $RFQ[$i]];

Or if keeping it how you have it now use the RFQ ID to get the correct array item from the Quantity array (if it exists) - I would probably use the other option.

I don't really do much PHP, but hope the above helps a little.
 
Thanks for the reply.

I managed to get around it by using a multidimensional array, ie:

PHP:
echo '<td><input type="checkbox" value="' . $rows['Part Number'] . '" name="RFQ[]"></td>'; 
        echo '<td>' . $rows['Part Number'] . '</td>'; 
        echo '<td>' . $rows['Manufacturer'] . '</td>'; 
        echo '<td>' . $rows['Stock'] . '</td>'; 
        echo '<td><input type="text" value="' . $_POST['Quantity'] . '" name="Quantity[' . $rows['Part Number'] . ']" tabindex="' . $tabindex++ .'" /></td>';

and
PHP:
for ($i=0; $i<$NumberRFQ; $i++) 
        { 
        echo '<tr>'; 
        echo '<td>' . $RFQ[$i] . '</td>'; 
        echo '<td>' . $Quantity[$RFQ[$i]][] . '</td>'; 
        echo '</tr>'; 
        }

Which works, but i am having a really odd issue.

Sometimes the Manufacturer hasnt been entered and so the cell is just empty. When i select a product that has no Manufacturer, and enter a quantity, it isnt always sent to the array?! I dont see any connection in the code between the two?

Say i select 3 items without a manufacturer, the first and last will show - not the second?! Try it for yourself here to see what i mean. Try searching for a 6202 and select the first two and last one.
 
I'm not sure i understand it but try using the row ids from the db.

<input name='select[]' value='". $rows['id'] ." type='checkbox'>

Then loop through the selected ones when its submitted

$select = $_POST['select'];

foreach ($select as $item){

select part name and manufactuer from db where id = $item
echo what you need

}
 
Back
Top Bottom