Page 1 of 1

[Noob] How to execute commands with checkbox arrays?

Posted: Mon Sep 13, 2010 3:16 pm
by TalonP
Hello!

My HTML form page looks like this:

Code: Select all

<form action="testping.php" method="post">
    Who do you want to ping?<br />
    <input type="checkbox" name="testPing[]" value="A" />Person A<br />
    <input type="checkbox" name="testPing[]" value="B" />Person B<br />
    <input type="checkbox" name="testPing[]" value="C" />Person C<br />
    <input type="checkbox" name="testPing[]" value="D" />Person D<br />
    <input type="checkbox" name="testPing[]" value="E" />Perseon E
 <input type="submit" name="formSubmit" value="Submit" />
</form>
I am having trouble with my php code (on testping.php, of course). I'm starting fresh with this:

Code: Select all

<?php
  $aPing = $_POST['testPing'];
  if(empty($aPing)) 
  {
    echo("You didn't select anyone.");
  } 
  else 
  {
    $N = count($aPing);
     for($i=0; $i < $N; $i++)
    {
      echo($aPing[$i] . " ");
    }
  }
?>
This is all I want it to do:

There is an array of checkboxes and a submit button. I already know how to use the php mail function-- that's not the issue. I just don't know where to PUT that function.
Depending on who the end-user checks before pressing submit, an e-mail (same body/headers but a different $to address for each) needs to be sent out to those people. If all 5 people are checked, 5 separate e-mails are sent. If Person B and D are checked-- etc etc.

Any help would be appreciated. I feel like I tried everything.

Thanks!

Re: [Noob] How to execute commands with checkbox arrays?

Posted: Mon Sep 13, 2010 3:45 pm
by AbraCadaver
Assuming the values of the check boxes are actually email addresses:

Code: Select all

  $aPing = $_POST['testPing'];
  if(empty($aPing))
  {
    echo("You didn't select anyone.");
  }
  else
  {
    foreach($aPing as $address)
    {
      mail($address, 'Ping', 'You have been pinged!');
    }
  }

Re: [Noob] How to execute commands with checkbox arrays?

Posted: Mon Sep 13, 2010 3:55 pm
by TalonP
Hey AbraCadaver,

Thanks for your awesomely fast response.

I thought of making the values of the checkboxes the e-mail address; however, that defeats the entire purpose of this specific script. I would like the e-mail addresses to remain secure and hidden in the php script that's running server-side, so that you can't view-source the HTML and see it.

I apologize for not having said that originally. Is there another way out? :lol:

Re: [Noob] How to execute commands with checkbox arrays?

Posted: Mon Sep 13, 2010 4:16 pm
by AbraCadaver
It would be better to save these in a database, but you can use a lookup array like so:

Code: Select all

$addresses = array('A'=>'me@here.com','B'=>'you@there.com'); //etc..

$aPing = $_POST['testPing'];
if(empty($aPing))
{
	echo("You didn't select anyone.");
}
else
{
	foreach($aPing as $value)
	{
		mail($addresses[$value], 'Ping', 'You have been pinged!');
	}
}

Re: [Noob] How to execute commands with checkbox arrays?

Posted: Mon Sep 13, 2010 5:03 pm
by TalonP
I NEVER would have gotten that.

Thank you so much for your help! It worked on the first attempt like a charm.