1

I am loading some data from a db table to checkboxes. When a user checks boxes and submits, the values from these checkboxes need to be added to a different table. With the code I have now, I am able to send values of one checked box. What am I missing in sending the values of all the checked checkboxes?

<table>
    <?php       
    $q5=mysqli_query($link,"SELECT * FROM brands_offer WHERE Brand_Id='$bid' AND Published='1' ");
    while($row5 = mysqli_fetch_array($q5)){
        $catid= $row5['Catg_Id'];
        $subcatid= $row5['Subcatg_Id'];
        $pid= $row5['Product_Id'];      
    ?><tr><td>
    <form action="store-admin.php?search=<?php echo $stname;?>#stock" method="post">
    <input type="checkbox" name="checkbox" value="<?php echo "$bname,$catid,$subcatid,$pid";?>" >
    <?php
        echo $bname;
        echo " ->  ";
        echo $catid;
        echo ",  ";
        echo $subcatid;
        echo ",  ";
        echo $pid;
        echo "  ";
    }
    ?></td></tr>
    <input type="submit" value="Save Changes" name="add" >
    </form>
    </table>
    <?php
    if(isset($_POST['add']))
    {
    $chk = $_POST['checkbox'];

    $val = explode(",",$chk);
    $bn = $val[0];
    $cid= $val[1];
    $scid= $val[2];
    $prid= $val[3];

    echo "<script type='text/javascript'>alert('brand: ". $bn."')</script>";
    echo "<script type='text/javascript'>alert('cat: ". $cid."')</script>";
    echo "<script type='text/javascript'>alert('sub: ". $scid."')</script>";
    echo "<script type='text/javascript'>alert('pr: ". $prid."')</script>";

    }?>

2 Answers 2

1

Use brackets in your checkbox name attribute name="checkbox[]" and your post variable will be an array of selected values.

Edit: I noticed you have form opening tag inside the while loop. You need to put it before while loop otherwise its generating tons of opening form tags.

Sign up to request clarification or add additional context in comments.

7 Comments

just name="checkbox[]" would suffice? no need to change in the if(isset where I'm getting the values?
@YohanBlake Yes no need to change, only thing you need is obviously loop the post variable to get all values.
yes, I tried including the POST code in the while loop, but it didn't work. How do I loop the POST variable?
@YohanBlake Look at updated answer and also show how you are looping POST variable.
okay I added the form open before the while loop and did name="checkbox[]" , now I'm not getting any values into the POST. I'm not sure how to loop the POST variable
|
0

So it looks like you have only one checkbox. If you want to POST the values of all the checkboxes as an array I believe you need to add [] to the name of your input field. So:

<input type="checkbox" name="checkbox[]" value="<?php echo "$bname";?>"/>
<input type="checkbox" name="checkbox[]" value="<?php echo "$catid";?>"/>
<input type="checkbox" name="checkbox[]" value="<?php echo "$subcatid";?>"/>
<input type="checkbox" name="checkbox[]" value="<?php echo "$pid";?>"/>

Hopefully I understood your question.

8 Comments

just name="checkbox[]" would suffice? no need to change in the if(isset where I'm getting the values?
@YohanBlake Not from what I can see. You override the post parameter when you use the same name for multiple input fields unless you append [] to the name. This loads the POST parameters as an "array" instead of "normal" key:value pairs.
He has multiple checkboxes generating through while loop.
Ah nice, didn't see that.
@YohanBlake, TheDrot commented first and his answer should suffice to be marked as correct.
|

Your Answer

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.