Results 1 to 4 of 4

Thread: Submit form that has dynamically generated checkboxes

  1. #1
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Submit form that has dynamically generated checkboxes

    I'm not well-versed in PHP but managed to dynamically generated a page/list of items, and their descriptions, images.... including a checkbox before each items. That page - page 1 - is ok.

    Code:
    <form name="compareForm" method="post" action="page2.php">
    <?php
    [loop]
    .....
    <input type="checkbox" value="<?php print $id ?>" name="compare[]"  />
    <a href="javascript:document.compareForm.submit();">Compare</a>
    [end loop]
    ?>
    </form>
    Viewing source shows that the Value of the checkboxes are generated correctly - which are the Item Ids - 1, 2, 3...

    However, if I check some checkboxes and submit the form, the values don't seem to be passed to page 2, which has the following codes:

    Code:
     <?php
      $compare = $_POST["compare"];
      foreach ($compare as $value) {
      print $value."<br />";}
      ?>
    Things that I tried:
    - Changed value="<?php print $id ?>" to hard-coded value="123": not work
    - Use only one Submit button at the end of the form instead of the Submit links under each item: not work

    Did I miss anything? or can we pass dynamically generated checkbox values?

    Thanks for the inputs!

  2. #2
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am having the same issue.
    my dynamically created checkbox is not being posted.
    looks like nobody knows about this.

  3. #3
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I have the same problem!
    I have javascript on my code but the javascript works fine, so I can't figure out the php either.

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    It works fine. Testing example:
    PHP Code:
    <?php
    if(isset($_POST['submit']))
        {
        
    $compare $_POST["compare"];
        echo 
    '<h2>Checkbox values:</h2>';
        foreach (
    $compare as $value)
            echo 
    $value."<br />";
        }
    ?>
    <form name="input" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <?php
    for($i=1;$i<=5;$i++)
        echo 
    '<input type="checkbox" value="test'.$i.'" name="compare[]"  />';
    ?>
    <br>
    <input type="submit" value="Submit" name="submit">
    </form>
    Checkbox/es needs to be checked to see the values.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

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
  •