Hello-- Checkbox (HTML) question

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Hello-- Checkbox (HTML) question

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something with "[]" in the name. PHP will automatically parse those as array elements.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post 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];
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use var_dump() on $_POST for an illustration of the structure.
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes. var_dump() was suggested to give you an illustration of how the information was stored by PHP, nothing more.
forum.samir
Forum Newbie
Posts: 7
Joined: Sat Aug 11, 2007 12:55 pm

Post by forum.samir »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply