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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts

    Multiple checkboxes using POST

    Dear all,

    I have an AJAX page that is getting results (usernames) from a database.

    Each result is displayed on the page, inside <form></form> tags, with a checkbox next to it.

    The goal is that you can select multiple names, by checking the corresponding boxes, then hit the submit button.

    After hitting submit, I want another PHP page to display all the selected names.

    How can I do that?

    If all my checkboxes input fields have the same name, it doesn't seem to work. If I give them different names, how can I know, on the display page, how many were checked and which should be displayed?

    Hope this is clear enough. Otherwise I can post some code. Let me know.

    Thanks in advance!
    Chuck Norris counted to infinity.
    Twice.

  • #2
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    OK I guess it will be clearer with the code... Here is the AJAX part.

    PHP Code:
    <?php
    header
    ("Cache-Control: no-cache, must-revalidate");
    // Date in the past
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    //functions and stuff missing here for clarity...

    // Fill up array with names

    $sql "select * from LNA_USER where HASTEAM='YES' order by PSEUDO asc";
    $req mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
    $rows mysql_num_rows($req);
    $i=0;
    while (
    $i $rows) {

        
    $data mysql_fetch_array($req);
        
    $a[] = removeaccents($data['PSEUDO']);
        
    $b[] = $data['ID_LNA_USER'];
        
    $i++;
    }

    //get the q parameter from URL
    $q=$_GET["q"];

    //lookup all hints from array if length of q>0
    if (strlen($q) > 0)
    {
      
    $hint="";
      for(
    $i=0$i<count($a); $i++)
      {
      if (
    strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
        {
        if (
    $hint=="")
          {
          
    $hint "<input type='checkbox' name='recipient' value='<?=$b[$i]?>'>" $a[$i] . "<br>";
          }
        else
          {
          
    $hint $hint "<input type='checkbox' name='recipient' value='<?=$b[$i]?>'>" $a[$i] . "<br>";
          }
        }
      }
    }

    // Set output to "no suggestion" if no hint were found
    // or to the correct values
    if ($hint == "") {
       
    $response "<p class='medium'>No suggestion!</p>";
    } else {
       
    $response $hint;
    }

    //output the response
    $formdsp "<form action='index.php?page=members/mail.php' method='post' ENCTYPE='x-www-form-urlencoded'>";
    $formdsp .= $response;
    $formdsp .= "<input type='hidden' name='action' value='addrecipients'>";
    $formdsp .= "<input type='submit' class='form' name='submit' value='Add recipients'>";
    $formdsp .= "</form>";
    echo 
    $formdsp;
    ?>
    Chuck Norris counted to infinity.
    Twice.

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Now on the mail.php page, I'd like to have something like...

    PHP Code:
    <?
    if ($_POST['action'] == "addrecipients") {
      
    // echo all $_POST['recipients'];
    }
    ?>
    Chuck Norris counted to infinity.
    Twice.

  • #4
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Hi,

    You need to change the name of the input type from recipient to recipient[], so it will store them in an array.

    Then you can just loop through the array like normal and display the ones stored in there

    PHP Code:
    foreach($_POST['box'] as $value){
    echo 
    "<h1/>User: ".$value."</h1>";


  • Users who have thanked Scriptet for this post:

    Arnaud (09-07-2009)

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Thanks!
    Chuck Norris counted to infinity.
    Twice.

  • #6
    New Coder
    Join Date
    Aug 2007
    Posts
    74
    Thanks
    3
    Thanked 2 Times in 2 Posts
    I think the simplest you way you could use is giving all checkboxes the same name as follows:

    PHP Code:
    <input type="checkbox" name="property[]" value="anything" />
    <
    input type="checkbox" name="property[]" value="anything1" />
    <
    input type="checkbox" name="property[]" value="anything2" />
    <
    input type="checkbox" name="property[]" value="anything3" />
    <
    input type="checkbox" name="property[]" value="anything4" /> 
    And what you get in php for $_POST["property"] is:

    Code:
    Array
    {
        0 => anything
        1 => anything1
        ... And so on.
    }
    And then you can traverse it's items with for example a foreach iterator.

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Yes, that was the above solution. Thank you.
    Chuck Norris counted to infinity.
    Twice.


  •  

    Posting Permissions

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