leoarce leoarce - 1 year ago 69
MySQL Question

How do I post two uneven arrays where one set is checkboxes and the other text input boxes

I can't get this to work. I need to update many records in 1 column, based on what's checked and filled out. I tried different combinations of checking and unchecking and having the text fields blank or not blank, and in this php post code i tried many different things, but can't figure out the correct combinations and if/elses or issets or empties, etc.

the values in the checkboxes correspond to record/row IDs. all the text boxes will be prefilled with prices. all the checkboxes will be dynamically checked or unchecked. a person can undo checked checkboxes if they want or check checkboxes that are not checked. on post, all the records that are checked should get the matching text box value.

the problem is i can't get the 2 arrays to match in my post. for example, in this sample set of fields, let's say i check the 2nd checkbox and the 4th checkbox. the records that should update and the values that should save into the column should be as follows...

2 -> 17.67
4 -> 19.84

but instead i get:

2 -> 16.95
4 -> 17.67

or this (if i remove the values from 1st and 3rd text boxes):

2 -> empty
4 -> 17.67

or this (2nd checkbox id and value missing completely)

4 -> 17.67

what am i doing wrong?

if (isset($_POST["savelist"]) && !empty($_POST["savelist"])) {
$productidcheckboxes = isset($_POST['productid']) ? $_POST['productid'] : array();
$listprices = isset($_POST['listprice']) ? $_POST['listprice'] : array();
//other things i tried
//$listprices = (empty($_POST['listprice'])) ? $_POST['listprice'] : array();
//$listprices = (!empty($_POST['listprice'])) ? $_POST['listprice'] : array();
//$productidcheckboxes = $_POST['productid'];
//$listprices = $_POST['listprice'];
$new = array();
for ($i=0; $i<count($productidcheckboxes); $i++) {
$new[] = $productidcheckboxes[$i];
$new[] = $listprices[$i];
foreach ($new as $value) {
$theid = $value;
$thelistprice = $value;
//different ifs i tried
//if ($theid<>"")
//if ($value<>"")
//if ($theid<>"" && $thelistprice<>"")
//if ($theid<>"" && $value<>"")
if ($thelistprice<>"")
echo $theid.": ";
echo $thelistprice."<br>";
//update table with the list prices
//mysql_query("UPDATE table_name SET mylistprices = '$thelistprice' WHERE id = $theid");
$theid = "";
$thelistprice = "";

form looks like this

<form action="" method="post">

<input type="checkbox" value="1" name="productid[]">
<input type="text" value="16.95" name="listprice[]">

<input type="checkbox" value="2" name="productid[]">
<input type="text" value="17.67" name="listprice[]">

<input type="checkbox" value="3" name="productid[]">
<input type="text" value="18.81" name="listprice[]">

<input type="checkbox" value="4" name="productid[]">
<input type="text" value="19.84" name="listprice[]">

<input type="checkbox" value="5" name="productid[]">
<input type="text" value="16.85" name="listprice[]">

<input type="submit" value="Save List" name="savelist">


by the way, by uneven i mean all the checkboxes will have values so correct rows will be updated, but the text boxes may or may not be filled. i would like it if i didn't have to clear any values in checkboxes or text inputs. it should just update records that are checked with it's corresponding values, and ignore non-checked checkboxes and the non-checked checkboxes corresponding values. but in the end, i may have to change how it's done, but i can't solve this one.

Answer Source

Add hardcoded numeric values to the form names so they match up in your processing page. Right now they are random:

<form action="" method="post">
    <input type="checkbox" value="1" name="productid[1]">
    <input type="text" value="16.95" name="listprice[1]">

    <input type="checkbox" value="2" name="productid[2]">
    <input type="text" value="17.67" name="listprice[2]">

    <input type="checkbox" value="3" name="productid[3]">
    <input type="text" value="18.81" name="listprice[3]">

    <input type="checkbox" value="4" name="productid[4]">
    <input type="text" value="19.84" name="listprice[4]">

    <input type="checkbox" value="5" name="productid[5]">
    <input type="text" value="16.85" name="listprice[5]">
    <input type="submit" value="Save List" name="savelist">

Now you know if the user checks product[4], it really is product[4]. When you leave your keys blank like productid[], that is just an anonymous spot in the array and makes it impossible to track when dealing with checkboxes that have no value unless checked.

If you check off productid[2] and productid[4] you know that the values in the listprice array are the values that go with what you have checked off:

    [listprice] => Array
            [1] => 16.95
            [2] => 17.67
            [3] => 18.81
            [4] => 19.84
            [5] => 16.85

    [productid] => Array
            [2] => 2
            [4] => 4


To access the values, loop through the productid but access the listprice:

foreach($_POST['productid'] as $key => $value){
    echo $_POST['listprice'][$value].'<br />';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download