Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    18
    Thanked 1 Time in 1 Post

    Question cart - product quantities problem

    Hi guys

    Could anyone tell me how to retrieve data from checkboxes that are generated dynamically on a base of how many records there is in the database ?

    PHP Code:
    while ($rows $query->fetch(PDO::FETCH_ASSOC)) {

    echo(
    "<label>$name</label><input type=\"text\" name=\"product[$id]\" />");


    I believe I would have to use some sort of loop like foreach, but I'm not sure how exactly
    Last edited by elem; 02-02-2013 at 10:09 PM.

  • #2
    Regular Coder student101's Avatar
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by elem View Post
    Hi guys
    Could anyone tell me how to retrieve data from checkboxes that are generated dynamically on a base of how many records there is in the database ?
    http://www.allthingsdiscussed.com/Mo...box-in-PHP.php
    Thanks for your support!
    Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

    Give thanks & resolve when done :thumbsup:

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    18
    Thanked 1 Time in 1 Post
    thanks mate but my problem is that these checkboxes are generated dynamically, so I don't know how many of them will be. I'm not that stupid

  • #4
    Regular Coder student101's Avatar
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts

    Question

    Quote Originally Posted by elem View Post
    thanks mate but my problem is that these checkboxes are generated dynamically, so I don't know how many of them will be. I'm not that stupid
    Why would u think u stupid?
    Anyway, wheres the code that generates these checkboxes?
    Your tiny bit of posted code shows u creating a textbox not a checkbox

    Edit:
    u need this or similar, adapt as needed.
    <input type="checkbox" name="checkbox" id="checkbox" />


    Edit: Do u only need the data retrieved or dynamically created too?


    try: (working blind here wrt your limited info on q)
    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
    <?php
        $chvalues = array();
        if(isset($_POST['checkbox']))    {
            foreach($_POST['checkbox'] as $ch => $value) {
                $chvalues[] = $value;
            }
        }
     
        while($row = mysql_fetch_array($result))
            if(in_array($row['first_name'], $chvalues)) {
                echo "<input name=\"checkbox[]\" type='checkbox' value='" . $row['first_name'] . "' checked='checked'/> ";
            } else {
               echo "<input name=\"checkbox[]\" type='checkbox' value='" . $row['first_name'] . "'/> ";
            }           
            echo $row['first_name'];
            echo "<hr />"; 
    }
     
        if(isset($_POST['checkbox'])) {
            foreach($_POST['checkbox'] as $checkbox => $val){
                echo $checkbox .':'.$val."<br />";
        } 
    ?>
    </form>
    Last edited by student101; 02-03-2013 at 05:51 PM.
    Thanks for your support!
    Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

    Give thanks & resolve when done :thumbsup:

  • Users who have thanked student101 for this post:

    elem (02-11-2013)


  •  

    Posting Permissions

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