Page 1 of 1
Hello-- Checkbox (HTML) question
Posted: Tue Aug 07, 2007 4:30 pm
by xterra
Ladies and gents,
I have emails.php, which is a list of unlimited email addresses, and each email has a "checkbox":
[ ]
email@email.com
[ ]
email@email.com
[x]
email@email.com
[ ]
email@email.com
The list count can be anything, since it's spit out from a database. When I submit it to another form for processing, how can I determine which emails are checked?
The html would be like this:
<form name=form1 method=post action=find_checkboxes.php>
<input type="checkbox" name=?????? value="
email@email.com"
<input type=submit name=Submit value=Find!>
What would I name it? There will be unlimited amount.
And what would I do on the server side??
$email=$_POST['email'];
But I don't know how many.
Thank you.
Posted: Tue Aug 07, 2007 4:43 pm
by feyd
something with "[]" in the name. PHP will automatically parse those as array elements.
Posted: Tue Aug 07, 2007 4:50 pm
by xterra
Ok I did that. Do you know how I can output it on the server side page now?
Some kind of loop.
$array[]=$_POST['array[]'];
while (unknown count?)
$i++;
echo $array[$i];
Posted: Tue Aug 07, 2007 5:35 pm
by feyd
use
var_dump() on $_POST for an illustration of the structure.
Posted: Tue Aug 07, 2007 6:46 pm
by xterra
Thanks. But if I didn't want to dump all the information, instead just loop through it and email each customer, can I use a loop?
Posted: Tue Aug 07, 2007 9:29 pm
by feyd
Yes.
var_dump() was suggested to give you an illustration of how the information was stored by PHP, nothing more.
Posted: Sat Aug 11, 2007 2:07 pm
by forum.samir
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
just try as follows:
Code: Select all
$total_email=mysql_num_rows($result_email);
$counter=1;
while($row=mysql_fetch_array($result_email)){
echo "<input type='chekcbox' name='check'.$counter value='{$row[email]}'>";
// checkbox name will be check1, check2 ...
counter++;
}
<input type='hidden' name='total_email' value='$total_email'> //just keep total email in a hidden field
After submitting the form, please try as follows:
Code: Select all
for($i=1;$i<=$count;$i++){
if(isset($_POST['check'.$i])) $email=$_POST['check'.$i]; // $email is a checked email
}
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sat Aug 11, 2007 8:36 pm
by superdezign
That is definitely overkill for checkboxes. Using the array as feyd suggested is a better choice. Checkboxes aren't posted if they aren't checked, so using the array would only return the checked boxes instead of having to traverse through every possible option.