View Full Version : Multiple checkboxes using POST

09-07-2009, 04:40 PM
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!

09-07-2009, 04:44 PM
OK I guess it will be clearer with the code... Here is the AJAX part.

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);
while ($i < $rows) {

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

//get the q parameter from URL

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
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>";
$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;

09-07-2009, 04:47 PM
Now on the mail.php page, I'd like to have something like...

if ($_POST['action'] == "addrecipients") {
// echo all $_POST['recipients'];

09-07-2009, 10:31 PM

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 :)

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

09-08-2009, 12:26 AM

Active X
09-08-2009, 10:16 AM
I think the simplest you way you could use is giving all checkboxes the same name as follows:

<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:

0 => anything
1 => anything1
... And so on.

And then you can traverse it's items with for example a foreach iterator.

09-08-2009, 10:33 AM
Yes, that was the above solution. Thank you.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum