roguewonder Posted August 15, 2014 Share Posted August 15, 2014 (edited) I keep getting the warning "unable to send" and yet I use everything in the for correctly. Please help me out. I have spent hours looking for an answer. It also posts the checkboxes I select at the bottom. I just want to be sent to the email, not necessarily echoed so I can view it. Thanks <?php // Functions to filter user inputs function filterName($field){ // Sanitize user name $field = filter_var(trim($field), FILTER_SANITIZE_STRING); // Validate user name if(filter_var($field, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+/")))){ return $field; }else{ return FALSE; } } function filterEmail($field){ // Sanitize e-mail address $field = filter_var(trim($field), FILTER_SANITIZE_EMAIL); // Validate e-mail address if(filter_var($field, FILTER_VALIDATE_EMAIL)){ return $field; }else{ return FALSE; } } function filterString($field){ // Sanitize string $field = filter_var(trim($field), FILTER_SANITIZE_STRING); if(!empty($field)){ return $field; }else{ return FALSE; } } // Define variables and initialize with empty values $nameErr = $emailErr = $messageErr = ""; $name = $email = $subject = $message = ""; // Processing form data when form is submitted if($_SERVER["REQUEST_METHOD"] == "POST"){ // Validate user name if(empty($_POST["name"])){ $nameErr = 'Please enter your name.'; }else{ $name = filterName($_POST["name"]); if($name == FALSE){ $nameErr = 'Please enter a valid name.'; } } // Validate email address if(empty($_POST["email"])){ $emailErr = 'Please enter your email address.'; }else{ $email = filterEmail($_POST["email"]); if($email == FALSE){ $emailErr = 'Please enter a valid email address.'; } } // Validate message subject if(empty($_POST["subject"])){ $subject = ""; }else{ $subject = filterString($_POST["subject"]); } // Validate user comment if(empty($_POST["message"])){ $messageErr = 'Please enter your comment.'; }else{ $message = filterString($_POST["message"]); if($message == FALSE){ $messageErr = 'Please enter a valid comment.'; } } // Check input errors before sending email if(empty($nameErr) && empty($emailErr) && empty($messageErr)){ // Recipient email address $to = 'youremail@example.com'; // Create email headers $headers = 'From: '. $email . "\r\n" . 'Reply-To: '. $email . "\r\n" . 'X-Mailer: PHP/' . phpversion(); // Sending email if(mail($to, $subject, $message, $Activity, $headers)){ echo '<p class="success">Your message has been sent successfully!</p>'; }else{ echo '<p class="error">Unable to send email. Please try again!</p>'; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Contact Form</title> <style type="text/css"> .error{ color: red; } .success{ color: green; } </style> </head> <body> <h2>Contact Us</h2> <p>Please fill in this form and send us.</p> <form action="" method="post"> <p> <label for="inputName">Name:<sup>*</sup></label> <input type="text" name="name" id="inputName" value="<?php echo $name; ?>"> <span class="error"><?php echo $nameErr; ?></span> </p> <p> <label for="inputEmail">Email:<sup>*</sup></label> <input type="text" name="email" id="inputEmail" value="<?php echo $email; ?>"> <span class="error"><?php echo $emailErr; ?></span> </p> <p> <label for="inputSubject">Subject:</label> <input type="text" name="subject" id="inputSubject" value="<?php echo $subject; ?>"> </p> <p> <label for="inputComment">Message:<sup>*</sup></label> <textarea name="message" id="inputComment" rows="5" cols="30"><?php echo $message; ?></textarea> <span class="error"><?php echo $messageErr; ?></span> </p> <p> <input type="checkbox" name="Activity[Sit]" value="Sit" /> value1 <br> <input type="checkbox" name="Activity[sloth]" value="Sloth" /> value2<br> <input type="checkbox" name="Activity[Run]" value="Run" /> value3 <br> </p> <input type="submit" value="Send"> <input type="reset" value="Reset"> </form> <?php if(!empty($_POST['Activity'])) { foreach($_POST['Activity'] as $check) { echo $check; //echoes the value set in the HTML form for each checked checkbox. //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5. //in your case, it would echo whatever $row['Report ID'] is equivalent to. } } ?> Edited August 15, 2014 by roguewonder Quote Link to comment https://forums.phpfreaks.com/topic/290458-multipel-checkboxes-not-working-in-form/ Share on other sites More sharing options...
requinix Posted August 15, 2014 Share Posted August 15, 2014 Have you checked your error log? It probably has an error message describing why mail() failed. Quote Link to comment https://forums.phpfreaks.com/topic/290458-multipel-checkboxes-not-working-in-form/#findComment-1487814 Share on other sites More sharing options...
roguewonder Posted August 15, 2014 Author Share Posted August 15, 2014 No error message in log file at all for that. Quote Link to comment https://forums.phpfreaks.com/topic/290458-multipel-checkboxes-not-working-in-form/#findComment-1487815 Share on other sites More sharing options...
requinix Posted August 15, 2014 Share Posted August 15, 2014 Check your mail settings in PHP, check your sendmail client's error logs... There is something out there that is wrong and I can't tell you what it is because I can't see your computer. Quote Link to comment https://forums.phpfreaks.com/topic/290458-multipel-checkboxes-not-working-in-form/#findComment-1487816 Share on other sites More sharing options...
roguewonder Posted August 15, 2014 Author Share Posted August 15, 2014 The mail settings are in the code I posted. So, you see nothing wrong in the code for the checkboxes? There is nothing externally wrong as I can receive mail fine with another pph script that does not have multiple checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/290458-multipel-checkboxes-not-working-in-form/#findComment-1487818 Share on other sites More sharing options...
fastsol Posted August 15, 2014 Share Posted August 15, 2014 Well of course it's not sending in the mail and echoing on the screen below, that's where the code for processing it is, at the bottom after all the other stuff that already processed. PHP reads the code from top to bottom, if things are in the right order, they won't process in the right order. Put this block of code under your Validate User Comment check and remove it from the bottom of the page. if(!empty($_POST['Activity'])) { $Activity = implode(', ', $_POST['Activity']); $message = $message."\r\n\r\nActivities Selected:".$Activity } Lastly, $Activity is not a mail() parameter, so take that out of there. You don't just list out the vars you want sent in the mail(). You need to build up the actual message to be sent and then use only the parameters that the mail() uses to send. You can read the php manual on how to use the mail() 1 Quote Link to comment https://forums.phpfreaks.com/topic/290458-multipel-checkboxes-not-working-in-form/#findComment-1487861 Share on other sites More sharing options...
roguewonder Posted August 15, 2014 Author Share Posted August 15, 2014 Thanks Fastsol, I didnt' hide my lack of php knowledge very well here. That's a very helpful source. Quote Link to comment https://forums.phpfreaks.com/topic/290458-multipel-checkboxes-not-working-in-form/#findComment-1487864 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.