TorQueMoD Posted March 16, 2013 Share Posted March 16, 2013 (edited) Hey all, So I've been at this all day yesterday and most of today and I'm about ready to smash my head through my monitor. I'm not a programmer by any means, but this code is simple enough that it should be working. Unfortunately it does not and I'm not knowledgeable enough to figure out why. I've got a form on my website that I want to use to collect information from people who decide to fill it out. The form can be seen at http://www.geekdomvancouver.com/register.html Here's the HTML code for the form: <!-- Registration Start --> <form onsubmit="return alertemail(r_email)" method="post" action="http://www.geekdomvancouver.com/send.php" name="registration_form" id="reg_form" $from_address = "info@geekdomvancouver.com";> <input type='hidden' name='rec_mailto' value='info@torquemod.com' /> <input type='hidden' name='rec_subject' value='Geekdom Registration' /> <input type='hidden' name='rec_thanks' value='thanks.html' /> <table width="50%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="19%">Full Name:</td> <td width="81%"><input type="text" name="name" id="name" /> (First and Last name)</td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" id="email" /> (Your email address)</td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>City:</td> <td><input type="text" name="city" id="city" /> (What city do you live in?) *only GVRD residents can win</td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Gender:</td> <td><select name="gender" id="gender"> <option>Male</option> <option>Female</option> <option selected="selected"></option> </select> (Are you male or female?)</td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><!-- Recaptcha Start --> <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LcZZd4SAAAAAJe-twy5RJyWbFYluHWLPuvQkQuB"> </script> <noscript> <iframe src="http://www.google.com/recaptcha/api/noscript?k=6LcZZd4SAAAAAJe-twy5RJyWbFYluHWLPuvQkQuB" height="300" width="500" frameborder="0"></iframe><br> <textarea name="recaptcha_challenge_field" rows="3" cols="40"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"> </noscript> <!-- Recaptcha End --></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><input type="reset" name="Reset" id="Reset" value="Reset" /></td> <td><input type="submit" name="Submit" id="Submit" value="I agree with the above stated rules. Please Submit my information" /></td> </tr> </table> </form> <!-- Registration End --> and the code for my send.php file: <?php require_once('recaptchalib.php'); $privatekey = "private key is in here but I'm not going to show it"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); } else { // define variables and initialize with empty values $nameErr = $emailErr = $cityErr = $genderErr = ""; $name = $email = $city = $gender = array(); if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Missing"; } else { $name = $_POST["name"]; } if (empty($_POST["email"])) { $emailErr = "Missing"; } else { $email = $_POST["email"]; } if (empty($_POST["city"])) { $cityErr = "Missing"; } else { $city = $_POST["city"]; } if (empty($_POST["gender"])) { $genderErr = "You must specify your gender"; } else { $gender = $_POST["gender"]; } } } ?> Thank you for submitting your application. Now my problem is twofold. Firstly, if I leave all of the form fields blank, it still proceeds to submit the form so validation is not working. Secondly, if I enter all of the information correctly, and press submit nothing happens. It parses the send.php file and displays the thank you message but no e-mail is sent. Is this because the send.php doesn't contain the form itself? Is there some way I'm supposed to pass the form info on to the send.php that I'm not doing? I thought that's what I was doing when defining the variables. Thanks! Edited March 16, 2013 by TorQueMoD Quote Link to comment https://forums.phpfreaks.com/topic/275750-form-submit-with-recaptcha-help/ Share on other sites More sharing options...
doddsey_65 Posted March 17, 2013 Share Posted March 17, 2013 (edited) As far as validation is concerned, you have the string "Thank you for submitting your application." outside of your if statement if ($_SERVER["REQUEST_METHOD"] == "POST") { which means that this will always be outputted to the browser.Instead of adding errors to a different variable each time add the to a single array. if (empty($_POST["name"])) { $errors[] = "Missing"; } Then you can do something like if (!empty($errors)){ die(var_dump($errors)); } Or output the errors however you would like. this would stop execution of the script if there are errors.Secondly, you say that the script is not sending an email on success, yet i see no mail() function which is an internal php function to send emails. Edited March 17, 2013 by doddsey_65 Quote Link to comment https://forums.phpfreaks.com/topic/275750-form-submit-with-recaptcha-help/#findComment-1419064 Share on other sites More sharing options...
TorQueMoD Posted March 17, 2013 Author Share Posted March 17, 2013 yes I realized the thankyou message was outside of the if statement. I figured that's where I'd put the thankyou page html info. What I was hoping to do is simply have a pop up message of some kind that would stop the form from submitting if the required fields are missing. As for the mail function, I didn't realize I needed to add one but of course that's obvious now that you mention it So right now all the form is doing is submitting to the send.php file but the send,php file needs to specify where to send the gathered information. I'll look into how to add that to the page. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/275750-form-submit-with-recaptcha-help/#findComment-1419069 Share on other sites More sharing options...
TorQueMoD Posted March 17, 2013 Author Share Posted March 17, 2013 (edited) Ok, I was able to get everything working using a new code base that I found. For anyone else who might be interested in doing something similar, here is the code: HTML Form Code: <!-- Registration Start --> <form method="post" action="http://www.YOURDOMAIN.com/send.php"> <table width="50%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="19%">First Name:</td> <td width="81%"><input type="text" name="first_name" id="first_name" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="last_name" id="last_name" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" id="email" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>City:</td> <td><input type="text" name="city" id="city" /> *Only GVRD residents can win</td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td>Gender:</td> <td><select name="gender" id="gender"> <option>Male</option> <option>Female</option> <option selected="selected"></option> </select></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><!-- Recaptcha Start --> <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=ENTER YOUR PUBLIC KEY HERE"> </script> <noscript> <iframe src="http://www.google.com/recaptcha/api/noscript?k=ENTER YOUR PUBLIC KEY HERE" height="300" width="500" frameborder="0"></iframe><br> <textarea name="recaptcha_challenge_field" rows="3" cols="40"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"> </noscript> <!-- Recaptcha End --></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><input type="reset" name="Reset" id="Reset" value="Reset" /></td> <td><input type="submit" name="Submit" id="Submit" value="I agree with the above stated rules. Please Submit my information" /></td> </tr> </table> </form> <!-- Registration End --> PHP Code for send.php: <?php // CAPTCHA Code Start require_once('recaptchalib.php'); $privatekey = "YOU PRIVATE CAPTCHA KEY HERE"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); } else { // CAPTCHA Code Finish // Submit Code Start if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "info@torquemod.com"; $email_subject = "Geekdom Registration"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['city']) || !isset($_POST['gender'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $city = $_POST['city']; // required $gender = $_POST['gender']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(strlen($city) < 2) { $error_message .= 'The City you entered do not appear to be valid.<br />'; } if(!preg_match($string_exp,$gender)) { $error_message .= 'Please specify your gender<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "City: ".clean_string($city)."\n"; $email_message .= "Gender: ".clean_string($gender)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- include your own success html here --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> This is the HTML code for your success/thank you page. </body> </html> <?php } } ?> Edited March 17, 2013 by TorQueMoD Quote Link to comment https://forums.phpfreaks.com/topic/275750-form-submit-with-recaptcha-help/#findComment-1419074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.