Reply LinkBack Thread Tools Search this Thread
Old 24-01-2008, 20:25   #1 (permalink)
Pixelate
Registered User
 
Join Date: Nov 2004
Posts: 3
fix form to send out 'thank you' email at signup?

Hello,

I have a signup form that is supposed to add a person to the DB and send them a 'you've signed up' email. I'm modifying someone else's form because I have to use some of the existing functionality of it.

I've got the form adding the person to the DB but the 'you've signed up' email isn't being sent out.
The following isn't all the form, so if you need all of it let me know.


Code:
//FORM GOES HERE... if (isset($_POST['SignUp'])) { $EmailAddress = $_POST['Email']; $FullName = $_POST['FullName']; $TheAge = $_POST['Age']; $DueDateMonth = $_POST['Month']; $DueDateDay = $_POST['Day']; $DueDateYear = $_POST['Year']; $DueDate = $DueDateYear."-".$DueDateMonth."-".$DueDateDay; $Result = mysql_query("SELECT * FROM EmailUsers WHERE EmailAddress = '".$EmailAddress."'"); if (mysql_num_rows($Result) == 0) { //Add the user and send intro email... $AddResult = mysql_query("INSERT INTO EmailUsers (EmailAddress, Name, DueDate, Age) VALUES ('".$EmailAddress."', '".addslashes($FullName)."', '".$DueDate."', ".$TheAge.")"); if ($AddResult) { echo 'Thank you for signing up with our email alerts program. A confirmation email has been sent to: <B>'.$EmailAddress.'</B>.<br /><br />theFunkystork.com staff.'; $Subject = "Welcome to theFunkystork.com - Track Your Pregnancy"; $Header = "Return-Path: track-your-pregnancy@thefunkystork.com\r\n"; $Header .= "From: theFunkystork <track-your-pregnancy@thefunkystork.com>\r\n"; //Grab the initial email to send to people (The Welcome Message). $Result = mysql_query("SELECT * FROM EmailAlerts WHERE Week = 0"); $Row = mysql_fetch_array($Result); $Message = stripslashes($Row['EmailText']); mail ($EmailAddress, $Subject, $Message, $Header); } else { //Unsuccessful - for some reason... } } else { echo 'The email address you entered (<b>'.$EmailAddress.'</b>) already exists in our database.'; } } else { //Show signup form... $CurrentYear = date('Y'); $NextYear = $CurrentYear + 1; $CurrentMonth = date('m'); $CurrentDay = date('d'); ?> <form name="TrackPregForm" method="post" action="pregnancy_email_thank_you"> <table border="0" cellpadding="3"> <tr><td align="right"> Your Name: </td><td> <input type="text" name="FullName" /> </td></tr> <tr><td align="right"> Your Age: </td><td> <input type="text" name="Age" size="5" maxlength="2" /> </td></tr> <tr><td align="right"> Your Email Address: </td><td> <input type="text" name="Email" /> </td></tr> <tr><td colspan="2" align="center"> <br /> <B>Enter Your Due Date</B> </td></tr> <tr><td align="right"> Month: </td><td> <select name="Month"> <?php $AllMonths = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); for ($x = 0; $x < 12; $x++) { if (($x + 1) == ((int) $CurrentMonth)) { if ($x < 9) { echo '<option value="0'.($x + 1).'" selected>'.$AllMonths[$x].'</option>'; } else { echo '<option value="'.($x + 1).'" selected>'.$AllMonths[$x].'</option>'; } } else { if ($x < 9) { echo '<option value="0'.($x + 1).'">'.$AllMonths[$x].'</option>'; } else { echo '<option value="'.($x + 1).'">'.$AllMonths[$x].'</option>'; } } } ?> </select> </td></tr> <tr><td align="right"> Day: </td><td> <select name="Day"> <?php for ($x = 1; $x < 32; $x++) { if ($x == ((int) $CurrentDay)) { if ($x < 10) { echo '<option value="0'.$x.'" selected>'.$x.'</option>'; } else { echo '<option value="'.$x.'" selected>'.$x.'</option>'; } } else { if ($x < 10) { echo '<option value="0'.$x.'">'.$x.'</option>'; } else { echo '<option value="'.$x.'">'.$x.'</option>'; } } } ?> </select> </td></tr> <tr><td align="right"> Year: </td><td> <select name="Year"> <?php //output this year and next... echo '<option value="'.$CurrentYear.'" selected>'.$CurrentYear.'</option>'; if ($CurrentMonth != '01' && $CurrentMonth != '02') //If it's not at least march, don't show the option for next year... { echo '<option value="'.$NextYear.'">'.$NextYear.'</option>'; } ?> </select> </td></tr> <tr><td colspan="2" align="center"> <input type="button" value="Track Your Pregnancy" onClick="validateTrackPreg();" /> </td></tr> </table> <input type="hidden" name="SignUp" value="True" /> </form> <?php } ?>


This is the Javascript to validate the form:
Code:
<script language="JavaScript" type="text/JavaScript"> <!-- //Form Validationg for Track Pregnancy... function validateTrackPreg() { var isValid = false; //Validate Name... var theName = document.TrackPregForm.FullName.value; var theAge = parseInt(document.TrackPregForm.Age.value); if (theName != "") { isValid = true; //Validate Age if (!(theAge) || theAge < 18) { isValid = false; alert('Please enter your age. (18 and older only).'); document.TrackPregForm.Age.focus(); } if (isValid) { //Validate Email Address... var theEmail = document.TrackPregForm.Email.value; var atLoc = theEmail.indexOf("@", 1); var dotLoc = theEmail.indexOf(".", atLoc + 2); var len = theEmail.length; if (atLoc > 0 && dotLoc > 0 && len > dotLoc + 2) { isValid = true; } else { isValid = false; //alert user and set focus to email... alert('Please enter a valid email address...'); document.TrackPregForm.Email.focus(); } //End Validate Email Address } if (isValid) { //Validate Due Date - Only allow a date that is at least one week in the future var currentDate = new Date(); var currentMonth = currentDate.getMonth() + 1; var currentDay = currentDate.getDate(); var currentYear = currentDate.getFullYear(); if (document.TrackPregForm.Year.value == currentYear) { //If it's this year, make sure the month & day are not in the past, and at least 1 week from today.. if (document.TrackPregForm.Month.value < currentMonth) { alert ('You cannot enter a due date from the past.\n\nPlease enter a due date that is at least one week from now.'); isValid = false; document.TrackPregForm.Month.focus(); } else if (document.TrackPregForm.Month.value == currentMonth && document.TrackPregForm.Day.value < (currentDay + 7)) { alert('You must enter a due date that is at least one week from now.'); isValid = false; document.TrackPregForm.Month.focus(); } else if ((document.TrackPregForm.Month.value - currentMonth) > 10) { alert('You cannot enter a due date more than 10 months in the future.'); isValid = false; document.TrackPregForm.Month.focus(); } } else { //If the due date is next year, only allow a due date that's 10 months or less in the future... if (((document.TrackPregForm.Month.value - currentMonth) + 12) > 10) { alert('You cannot enter a due date more than 10 months in the future.'); isValid = false; document.TrackPregForm.Month.focus(); } } if ((document.TrackPregForm.Month.value == 4 || document.TrackPregForm.Month.value == 6 || document.TrackPregForm.Month.value == 9 || document.TrackPregForm.Month.value == 11) && document.TrackPregForm.Day.value > 30) { alert('Please enter a valid date.'); isValid = false; document.TrackPregForm.Month.focus(); } else if (document.TrackPregForm.Month.value == 2 && document.TrackPregForm.Day.value > 28) { //Check to see if it's a leap year.... var remainder = (currentYear - 2004) % 4; //2004 was the most recent leap year. if (remainder < 1) //It's a valid leap year. { //make sure day is only 29 if (document.TrackPregForm.Day.value != 29) { alert('Please enter a valid date.'); isValid = false; document.TrackPregForm.Month.focus(); } } else { alert('Please enter a valid date.'); isValid = false; document.TrackPregForm.Month.focus(); } } //End Validate Due Date } } else { isValid = false; //alert user and set focus to Name... alert('Please enter your name.'); document.TrackPregForm.FullName.focus(); } if (isValid) { //Submit Form... document.TrackPregForm.submit(); } } //Form Validationg for Track Pregnancy Opt Out... function validateOptOut() { isValid = true; //Validate Email Address... var theEmail = document.OptOutForm.Email.value; var atLoc = theEmail.indexOf("@", 1); var dotLoc = theEmail.indexOf(".", atLoc + 2); var len = theEmail.length; if (atLoc > 0 && dotLoc > 0 && len > dotLoc + 2) { isValid = true; } else { isValid = false; //alert user and set focus to email... alert('Please enter a valid email address...'); document.OptOutForm.Email.focus(); } //End Validate Email Address if (isValid) { //Submit Form... document.OptOutForm.submit(); } } //--> </script>
  Reply With Quote
Old 24-01-2008, 21:50   #2 (permalink)
pgo
i'm done, son
 
Join Date: Jan 2005
Posts: 12,262
First, you need to make sure your server can send mails with the mail function. If you're testing locally, you'll need to set up an SMTP connection in php.ini, I believe.

Second, try echoing your four variables that are going into the mail() function to make sure they're all set correctly.

PHP Code:
echo "Email: " $EmailAddress "<br /><br />";
echo 
"Subject: " $Subject "<br /><br />";
echo 
"Message: " $Message "<br /><br />";
echo 
"Header: " $Header "<br /><br />"
Or you can test the mail() function (I do something similar to this usually):

PHP Code:
if (mail ($EmailAddress$Subject$Message$Header)) {
  echo 
"Yay, mail worked!";
}
else {
  echo 
"Hmmm.  That's not good.";

Otherwise, I don't know.

Also, as an aside, it's generally considered good practice to make your data safe before you just throw it in a database. Looks to me like you're wide open for any number of MySQL injection attacks and other malfeasance.

And validating forms with JavaScript for any purpose other than enhancing usability (read: not obnoxious alert boxes) is pretty much pointless.
  Reply With Quote
Old 25-01-2008, 06:09   #3 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,659
Ya, if someone has javascript turned on, their information will not be validated, and you can get some nasty stuff put into your system. Google "email injections" and "php form validation".

As for your code, at a very quick glance it seemed like it should be working, so its very potentially a problem with your server. You should try out the phpmailer class (google it), and use it to send your mails using SMTP. Its fairly straightforward to use, and there are tutorials on the site. Your email is potentially getting blocked by junk mail filters.
  Reply With Quote
Old 28-01-2008, 02:48   #4 (permalink)
Pixelate
Registered User
 
Join Date: Nov 2004
Posts: 3
Thanks for the reply.

I ended up getting it to work. I went back to the original code and left more in the modified version and it worked. I may have left something out when I modified the form the first time.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8