October 15, 201213 yr Hi all, having a bit of trouble working out how to send a YES / NO value from a tickbox form, its basically for people to opt in / out of a newsletter. Any help would be greatly appreciated. Heres the HTML form: <form action="send_form_email.php" method="post" name="contactform"> <label for="first_name">First Name </label> <input type="text" name="first_name" maxlength="50" size="30" /> <label for="last_name">Last Name </label> <input type="text" name="last_name" maxlength="50" size="30" /> <label for="email"> Email Address </label> <input type="text" name="email" maxlength="80" size="30" /> <label for="telephone"> Telephone </label> <input type="text" name="telephone" maxlength="30" size="30" /> <label for="comments">Enquiry<br></label> <textarea name="comments" cols="44" rows="9"></textarea> <br> [b]<label for="newsletter">May we contact you regarding future offers?</label>[/b] [b] <input id="checkbox" type="checkbox" name="vehicle" value="1">[/b] <input type="submit" id="submit" value="Submit" /> </form> -------------------------------- and the php include <?php if(isset($_POST['email'])) { // Email and Subject $email_to = "info@makebelievemedia.co.uk"; $email_subject = "SDS Website enquiry"; 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['telephone']) || !isset($_POST['comments'])) { 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 $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // 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($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<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 .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; $email_message .= "Newsletter: ".clean_string($newsletter)."\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); $headers2 = 'From: '.$email_to."\r\n". 'X-Mailer: PHP/' . phpversion(); $replymsg = "Thank you for your request."; mail($email_from, $email_subject, $replymsg, $headers2); ?> I added $email_message .= "Newsletter: ".clean_string($newsletter)."\n"; which shows the Newsletter header in the sent email but i cant work out how to send a value from the tick box. any advice? James Edited October 15, 201213 yr by Renaissance-Design The forum has code formatting for a reason...
October 15, 201213 yr Author its ok iv figured it out.. just needed the follow line and value of form changed. $email_message .= $_POST['newsletter'] == "unscribe" ? "Newsletter: Yes \n" : "Newsletter: No \n"; one thing i would like to do which may be a bit out my league is to automatically add the email address of people who selected 'yes' to the newsletter mailing list. i was thinking of going down the mailchimp route but would rather figure out a way to do myself Edited October 15, 201213 yr by Renaissance-Design Code formatting. Again.
Create an account or sign in to comment