Jump to content

Sending emails to multiple checkbox selections


fat creative

Recommended Posts

I am pulling my hair out at this point and have tried everything I can think of.  Any help would be so very appreciated.

 

I have a form that selects from a database based on the state selected.  It then queries my database and returns all the results where state equals whatever was selected and it creates the page congress.php. When it returns the selection, it creates a table where it displays the name, email, and phone number along with a checkbox.  I have given each checkbox a unique name equal to the ID number of that record. What I am trying to do is to send an email based on whether the checkbox is selected or not.  I cannot for the life of me figure out how to get it to do this.  I have this information in a form and when the form is submitted it calls a file sendmail.php.

 

I had this all working earlier today where it would send an email if you clicked on the persons name.  The client has requested the ability to send to all people if they are selected instead and this project is due Monday.  In seeing that I don't have a lot of time, I gave up trying to figure this out and decided I'd just add in EVERY possible variable for the checkbox (I know not the right  or best way, but I am really short on time) and I tried to put in a statement that says if the radio box equals on, then query the database for all records where the ID equals some number and echo the email.  What I was hoping to do is to get all the emails for the checkboxes that are on and somehow append them all together.

 

I am SURE this is a completly backwards way of doing this, but I am pretty new to PHP. I am quite pleased with myself for what I did figure out so far on my own, but I am really stumped at this point.

 

Please please please, can anyone offer some suggestions. And suggestions that a newbie would understand :-).

 

This is my code from sendmail.php. Right now all I can see is that the checkbox is returning "on".

# GRAB THE VARIABLES FROM THE FORM

 

 

$radio126 = mysql_real_escape_string($_POST['126']);

 

 

if ($radio126 == "on")

{

 

$query = "SELECT * FROM Sheet1 WHERE ID == '126'";

echo $email;

}

else

{

echo "radio is not selected";

}

echo $radio126;

echo "<br />";

 

 

$query = "SELECT * FROM Sheet1";

 

$result = mysql_query($query)

 

    or die ("Couldnt execute query. It's not working.");

 

 

 

?>

thank you (this I put in just to see it return something)

 

 

This is the code from the congress.php file.

$query = "SELECT * FROM Sheet1";

 

$result = mysql_query($query)

 

    or die ("Couldnt execute query. It's not working.");

 

 

# GRAB THE VARIABLES FROM THE FORM

 

$state = mysql_real_escape_string($_POST['state']);

$fname = mysql_real_escape_string($_POST['fname']);

$lname = mysql_real_escape_string($_POST['lname']);

$address1 = mysql_real_escape_string($_POST['address1']);

$address2 = mysql_real_escape_string($_POST['address2']);

$city = mysql_real_escape_string($_POST['city']);

$zip = mysql_real_escape_string($_POST['zip']);

$useremail = mysql_real_escape_string($_POST['useremail']);

 

 

 

//this should select based on form selections

$query = "SELECT * FROM Sheet1";

 

$query = "SELECT * FROM Sheet1 WHERE state LIKE '%$state%'";

echo $state;

echo "<br /><br />";

 

 

 

$result = mysql_query($query)

 

    or die ("There was an error:<br />".mysql_error()."<br />Query: $query");

 

 

 

if(!mysql_num_rows($result)) //if none!!!

 

 

  echo "There were no records returned from the database";

 

}

 

else //otherwise draw the table

 

{

 

  //put info on new lines

  echo "<form action=\"http://www.mentholchoice.com/sendmail.php?$useremail\" method=\"post\">";

  echo "Click on a congressman to send email <br /><br /><br />";

 

  echo "<table cellspacing=\"15\">\n";

 

  while($row = mysql_fetch_assoc($result))

 

  {

 

      extract($row);

 

   

      echo "<tr><td valign=\"top\">";

 

      echo "<td valign=\"top\"><div align=\"left\"><b><span class=\"style5\">$name</span></b></div>";

 

      echo "<td valign=\"top\"><span class=\"style12\">$party</span>";

 

      echo "<td valign=\"top\"><span class=\"style12\">$district_phone</span>";

 

      echo "<td valign=\"top\"><span class=\"style12\">$dc_phone</span>";

 

$radio = $ID;

 

if ($email == "NA")

{

      echo "<td valign=\"top\"><span class=\"style12\"></span>";

}

else

{

      echo "<td valign=\"top\"><span class=\"style12\"><input type=\"checkbox\" name=\"$radio\" id=\"sendto\" /></span>"; 

}

 

 

      echo "<td valign=\"top\"><div align=\"left\"><b><span>$email $ID</span></b></div></div></a>";

 

 

      echo "</td></tr>\n";

 

  }

 

}

 

 

echo "</table>\n";

 

 

echo "  <label>";

echo "  <input type=submit name=\"Send email\" id=\"Send email\" value=\"Submit\" />";

echo "  </label>";

echo "</form>";

 

 

?> 

 

the site is currently posted at http://www.mentholchoice.com/takeaction.html

Thank you so much again to anyone who can help!!!!!

Link to comment
Share on other sites

If you name the checkboxes like so:

 

<input type="checkbox" name="id[<?php echo $id; ?>]" value="1" />

 

EX:

 

<input type="checkbox" name="id[1]" value="1" /> Mike <br />
<input type="checkbox" name="id[2]" value="1" /> John <br />
<input type="checkbox" name="id[3]" value="1" /> George <br />
<input type="checkbox" name="id[4]" value="1" /> Jane <br />
<input type="checkbox" name="id[5]" value="1" /> Sarah <br />

 

 

once submitted, your $_POST data will look like this:

 

<?php
$_POST['id'] = array(
'1' => '1',
'2' => '1',
'3' => '1',
'4' => '1',
'5' => '1'
);
?>

The 1/2/3/4/5 are the IDs of the checkboxes that were selected.

 

You can loop through this data, and send it to each email:

 

(pseudo-code)

<?php
foreach($_POST['id'] as $id=>$one){

  //SQL
  query("select email from __ where ID = $id");
  $email = getRow();

  mail($email, ..... );

}
?>

 

Link to comment
Share on other sites

Thank you for the suggestions on how to fix this the RIGHT way!

 

I have updated my code, but it's giving me an error message that I can't get past. I was getting a few along the way and

had to make some minor changes, so I hope I didn't mess up the logic of what you sent.  The error message is coming up after

calling the sendmail.php page.

 

I would be so grateful for any other help.

 

The congress.php file is now updated as follows:

 

# GRAB THE VARIABLES FROM THE FORM

 

$state = mysql_real_escape_string($_POST['state']);

$fname = mysql_real_escape_string($_POST['fname']);

$lname = mysql_real_escape_string($_POST['lname']);

$address1 = mysql_real_escape_string($_POST['address1']);

$address2 = mysql_real_escape_string($_POST['address2']);

$city = mysql_real_escape_string($_POST['city']);

$zip = mysql_real_escape_string($_POST['zip']);

$useremail = mysql_real_escape_string($_POST['useremail']);

 

//this will email the form from the takeaction.html page

$mailTo = "jfolkes@fatcreative.com";

$msgSubject = "Menthol Choice email";

$msgBody = "$salutation, $fname, $lname, $useremail, $address1, $address2, $city, $state, $zip, $country, $age, $email_pref";

$xHeaders = "From: $useremail\nX−Mailer: PHP/" . phpversion();

mail ($mailTo, $msgSubject, $msgBody, $xHeaders);

 

 

 

 

//this should select based on form selections

$query = "SELECT * FROM Sheet1";

 

$query = "SELECT * FROM Sheet1 WHERE state LIKE '%$state%'";

echo $state;

echo "<br /><br />";

 

 

 

$result = mysql_query($query)

 

    or die ("There was an error:<br />".mysql_error()."<br />Query: $query");

 

 

 

if(!mysql_num_rows($result)) //if none!!!

 

 

  echo "There were no records returned from the database";

 

}

 

else //otherwise draw the table

 

{

 

  //put info on new lines

  echo "<form action=\"http://www.mentholchoice.com/sendmail.php?$useremail\" method=\"post\">";

  echo "Click on a congressman to send email <br /><br /><br />";

 

  echo "<table cellspacing=\"15\">\n";

 

  while($row = mysql_fetch_assoc($result))

 

  {

 

      extract($row);

 

     

      echo "<tr><td valign=\"top\">";

 

      echo "<td valign=\"top\"><div align=\"left\"><b><span class=\"style5\">$name</span></b></div>";

 

      echo "<td valign=\"top\"><span class=\"style12\">$party</span>";

 

      echo "<td valign=\"top\"><span class=\"style12\">$district_phone</span>";

 

      echo "<td valign=\"top\"><span class=\"style12\">$dc_phone</span>";

 

$radio = $ID;

 

if ($email == "NA")

{

      echo "<td valign=\"top\"><span class=\"style12\"></span>";

}

else

{

      echo "<td valign=\"top\"><span class=\"style12\"><input type=\"checkbox\" name=\"id[<?php echo $id; ?>]\" value=\"1\" /></span>"; 

}

 

 

      echo "<td valign=\"top\"><div align=\"left\"><b><span>$email $ID</span></b></div></div></a>";

 

 

      echo "</td></tr>\n";

 

  }

 

}

 

 

echo "</table>\n";

 

 

echo "  <label>";

echo "  <input type=submit name=\"Send email\" id=\"Send email\" value=\"Submit\" />";

echo "  </label>";

echo "</form>";

 

 

?> 

 

And the sendmail.php file has been updated as follows:

 

# GRAB THE VARIABLES FROM THE FORM

 

$_POST['id'] = array(

'1' => '1',

'2' => '1',

'3' => '1',

'4' => '1',

'5' => '1'

);

 

foreach($_POST['id'] as $id=>$one){

 

  //SQL

  $query = ("select email from Sheet1 where ID = $id");

  $email = getRow();

$sent = mail($email) ;

if($sent)

{print "Your mail was sent successfully"; }

else

{print "We encountered an error sending your mail"; }

 

}

 

 

$query = "SELECT * FROM Sheet1";

 

$result = mysql_query($query)

 

    or die ("Couldnt execute query. It's not working.");

 

 

 

?>

thank you

 

Link to comment
Share on other sites

Er, there seems to be a misunderstanding.

 

That pseudo-code wasn't meant to be used as is.

 

Where it says "getRow()", your actually supposed to fetch the row with mysql_fetch_assoc (or similar).

 

this part:

$_POST['id'] = array(
'1' => '1',
'2' => '1',
'3' => '1',
'4' => '1',
'5' => '1'
);

is not supposed to be used at all. It is just an explanation of what the data looks like, after the user submits the form.

 

Before I can offer any real code, I need to know 2 things:

1) What is the error ?

2) is this the flow that the user takes ?

 

    i) User fills in form at takeaction.html

    ii) Form submits to congress.php

  iii) congress.php generates a form which submits to sendmail.php

 

If that is incorrect, please describe what is happening.

Link to comment
Share on other sites

Sorry, I haven't worked with arrays so I didn't really know what that was. I watched some video tutorials at killerphp.com last night, so I think I understand what to do, I just need to change the options to whats in my data. So I have id 126 ->email@email.com and I do that for the entire list.  But I still don't understand what to do with it next.

 

As far as the flow, yes that is correct in terms of what I can't get to work.  The start of congress.php also emails the form data but I think I have that part working.  On the congress.php form, I also need to add in a select all option, but I THINK I know how to do that.

 

Late last night, I did come up with a very backwards way of getting this to work so I feel less pressure for the Monday deadline, but I'd still like to know how to do it the RIGHT way, so again, any other help would be appreciated.

 

Thanks again!

Link to comment
Share on other sites

<?php
#congress.php

while($row = mysql_fetch_assoc($result)){

	extract($row);
	echo "<tr><td valign=\"top\">";
	echo "<td valign=\"top\"><div align=\"left\"><b><span class=\"style5\">$name</span></b></div>";
	echo "<td valign=\"top\"><span class=\"style12\">$party</span>";
	echo "<td valign=\"top\"><span class=\"style12\">$district_phone</span>";
	echo "<td valign=\"top\"><span class=\"style12\">$dc_phone</span>";

	if ($email == "NA"){
			echo "<td valign=\"top\"><span class=\"style12\"></span>";
	}else{
			echo "<td valign=\"top\"><span class=\"style12\"><input type=\"checkbox\" name=\"id[$ID]\" value=\"1\" /></span>"; 
	}
	echo "<td valign=\"top\"><div align=\"left\"><b><span>$email $ID</span></b></div></div></a>";
	echo "</td></tr>\n";
}
?>   

<?php
# And the sendmail.php file has been updated as follows:
foreach($_POST['id'] as $id=>$one){
$query = mysql_query(
	sprintf(
		"SELECT email FROM Sheet1 WHERE ID = %d",
		$id
	)
);
$row = mysql_fetch_assoc($query);
$sent = mail($row['email'], "subject", "message"); // You must update these to send a real email. (Similar to what you did in congress.php
if($sent){
	print "Your mail was sent successfully";
}else{
	print "We encountered an error sending your mail";
}
}
?>

I just fixed a few things for you that should help you out.

Link to comment
Share on other sites

mrdamien, thank you very much for all the help!  Right now, I have the site done in my backwards way of doing it and since its

due tomorrow, I'm giving it to the client this way. What I ended up doing was writing an if statement to see if each checkbox

is "on".  If it was, it sends the email. Then it checks the next checkbox. While it works, I know its not the best way as I had

to create almost 500 if statements, one for each possible checkbox that could be on the page.

 

However, I am making a copy of everything to play with on my own so i can learn how to do it the right way for the next

project.  The client did mention possibly updating some things later in the week and hopefully I can update the site with the

better way. 

 

Thank you again, so very much. I have learned quite a bit with this project and am looking forward to the day that I can help

others with their problems!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.