Results 1 to 3 of 3

Thread: Paasing Multiple Array Checkbox Values To Mysql

  1. #1
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Paasing Multiple Array Checkbox Values To Mysql

    Hi All,

    I am struck with checkbox array issue. Below is my code



    if(isset($_POST['submit']))
    {
    $amount = $_POST['customerid'];
    $agent = $_POST['name'];
    $amount = $_POST['amount'];


    // here i want to to update my table based on customerid. I am unable to pass the values in a loop to table


    }


    <table border = 1 align="center">
    <?php
    while($result = mysql_fetch_array($query))
    {
    ?>
    <tr>
    <td bgcolor="#666666">
    <input name="checkbox[]" type="checkbox" id="checkbox[]" value="0" />
    </td>
    <td align="center"><input name="customerid[]" type="text" id="customerid" value="<?php echo $result['customer_id']; ?>"></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<?php echo $result['name']; ?>"></td>
    <td align="center"><input name="amount[]" type="text" id="amount" value=" "></td>



    </tr>

    <?php

    }
    ?>
    <input type="submit" name="submit" value="Submit" />
    </table>


    Anyone can help me in this issue.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Ok, well the problem here is that all of the related form fields with an array in the name must match their respective checkbox. In lamens terms, everything related to a set checkbox must be set, and nothing else.

    What would work is passing an iterative count to the parameter of the array to mark which field should be set. Then you can easily get their name by their count.


    So your HTML should look like:
    PHP Code:
    <table border = 1 align="center">
    <?php 
    while($result mysql_fetch_array($query))
    {
    $c++;
    ?>
    <tr>
    <td bgcolor="#666666">
    <input name="checkbox[<?php echo $c?>]" type="checkbox" id="checkbox[<?php echo $c?>]" value="<?php echo $c?>" />
    </td> 
    <td align="center"><input name="customerid[<?php echo $c?>]" type="text" id="customerid" value="<?php echo $result['customer_id']; ?>"></td>
    <td align="center"><input name="name[<?php echo $c?>]" type="text" id="name" value="<?php echo $result['name']; ?>"></td>
    <td align="center"><input name="amount[<?php echo $c?>]" type="text" id="amount" value=" "></td>



    </tr>

    <?php 

    }
    ?>
    <input type="submit" name="submit" value="Submit" />
    </table>
    And your iterative loop should look something like this:
    PHP Code:
    if(isset($_POST['submit']))
    {


    // here i want to to update my table based on customerid. I am unable to pass the values in a loop to table

    foreach($_POST[checkbox] as $chk) {
    $amount $_POST['customerid'][$chk];
    $agent $_POST['name'][$chk];
    $amount $_POST['amount'][$chk];
    }


    By assigning the value of the checkbox to the current count, and having all of the other fields with the same count, that checkbox can now be associated with those form fields.
    - Josh

  3. The Following User Says Thank You to JShor For This Useful Post:

    hemi519 (07-18-2012)

  4. #3
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    thank u very much, its working fine now

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •