[PHP] Email contact script - parsing error, parsing error with my "contact us" script |
[PHP] Email contact script - parsing error, parsing error with my "contact us" script |
Armitage2k |
Jul 7 2014, 03:20 AM
Post
#1
|
Group: Members Posts: 4 Joined: 7-July 14 Member No.: 21,206 |
Hello everyone,
I am currently reviving my html / PHP skills after not using them at all for the past 6-7 years. With this in mind, I decided to try myself at a simple email script that I just cant get to work, especially since it doesnt even return any error.. My form is supposed to have somone enter his name and department, followed by the problem nature and a short description. I use this script to generate an email sent to a distribute group who then will follow up some action. This is my form CODE <html> <head> <title>Problem Alert</title> <link rel="shortcut icon" href="./images/favicon.ico"> </head> <body> <font size="2"> <table align="left" width="300px" bgcolor="#DDD9C4"> <td> <tr> <img src="./images/iclogo_small.jpg" style="width: 300px"/><br /> <b>Problem Alert Form</b> <br /> </tr> <br /> <tr> <form name="contactform" action="./scripts/phpFormMailer/process2.php" method="post"> <div> <label for="name">Name:</label><br /> <input type="text" id="name" /> </div> <div> <label for="department">Department:</label><br /> <select name="department" style="width: 300px"> <option value="blank"> </option> <option value="FO">Front Office</option> <option value="F&B">Food & Beverage</option> <option value="HSKP">Housekeeping</option> <option value="S&M">Sales & Marketing</option> <option value="HR">Human Resources</option> </select </div> <div> <label for="problem">Problem:</label><br /> <select name="problem" style="width: 300px"> <option value="blank"> </option> <option value="Check In - Dirty / Occupied Room">Check In - Dirty / Occupied Room</option> </select </div> <div> <label for="priority">Priority:</label><br /> <select name="priority" style="width: 300px"> <option value="blank"> </option> <option value="Low">Low</option> <option value="Medium">Medium</option> <option value="High">High</option> </select> </div> <br /> </tr> <br /> <tr> <div> <label for="gst_name">Guest Name:</label><br /> <input type="text" id="gst_name" /> </div> <div> <label for="gst_room">Guest Room:</label><br /> <input type="text" id="gst_room" /> </div> <div> <label for="comments">Comments:</label><br /> <textarea style="width: 300px; height: 150px;" id="comments"></textarea> </div> <div> <input type="submit" value="Submit" /><br /><br /> </div> </form> </tr> </td> </font> </table> </body> </html> and below my parser: CODE <html> <head> </head> <body> <?php if(isset($_POST['name'])) { // CHANGE THE TWO LINES BELOW $email_to = "myemail@gmail.com"; $email_from = "donotreply@thismail.com"; $email_subject = "Problem Alert"; 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['name']) || !isset($_POST['department']) || !isset($_POST['problem']) || !isset($_POST['priority']) || !isset($_POST['gst_name']) || !isset($_POST['gst_room']) || !isset($_POST['comments']) ) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $name = $_POST['name']; // required $department = $_POST['department']; // required $problem = $_POST['problem']; // required $priority = $_POST['priority']; // not required $gst_name = $_POST['gst_name']; // not required $gst_room = $_POST['gst_room']; // not required $comments = $_POST['comments']; // required $date = date("F j, Y, g:i a"); $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 = "Kindly be informed that a Guest Problem Alert has been raised. Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $date .= "Date: ".($date)."\n"; $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Department: ".clean_string($department)."\n"; $email_message .= "Problem: ".clean_string($problem)."\n"; $email_message .= "Priority: ".clean_string($priority)."\n"."\n"; $email_message .= "Guest Name: ".clean_string($gst_name)."\n"; $email_message .= "Guest Room: ".clean_string($gst_room)."\n"; $email_message .= "Comments: ".clean_string($comments)."\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); ?> <!-- success message --> <font> Thank you for submitting the alert. Please call RAM to raise awareness as well.</font> <?php } die(); ?> </body> </html> There is no error message and the success message is also not displayed. any idea what could have gone wrong here? I tried a free script from one of the many script-websites, but that one gave me an SMTP error... Not sure if this indicates any problem with my server. Thanks alot! A2k |
CharlesEF |
Jul 7 2014, 03:47 AM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Place these commands at the top of your script. Hopefully, they will display the error messages.
ini_set('display_errors', 'On'); error_reporting(E_ALL); |
Armitage2k |
Jul 7 2014, 04:36 AM
Post
#3
|
Group: Members Posts: 4 Joined: 7-July 14 Member No.: 21,206 |
Hi,
thanks. Now it shows my error functions... CODE "; echo $error." "; echo "Please go back and fix these errors. "; die(); } // validation expected data exists if ( !isset($_POST['name']) || !isset($_POST['department']) || !isset($_POST['problem']) || !isset($_POST['priority']) || !isset($_POST['gst_name']) || !isset($_POST['gst_room']) || !isset($_POST['comments']) ) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $name = $_POST['name']; // required $department = $_POST['department']; // required $problem = $_POST['problem']; // required $priority = $_POST['priority']; // not required $gst_name = $_POST['gst_name']; // not required $gst_room = $_POST['gst_room']; // not required $comments = $_POST['comments']; // required $date = date("F j, Y, g:i a"); $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; $string_exp = "/^[A-Za-z .'-]+$/"; if (!preg_match($string_exp,$name)) { $error_message .= 'The name you entered does not appear to be valid. '; } if (!preg_match($string_exp,$problem)) { $error_message .= 'The name you entered does not appear to be valid. '; } if (!preg_match($string_exp,$department)) { $error_message .= 'The Last Name you entered does not appear to be valid. '; } if (strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid. '; } if (strlen($error_message) > 0) { died($error_message); } $email_message = "Kindly be informed that a Guest Problem Alert has been raised. Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $date .= "Date: ".($date)."\n"; $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Department: ".clean_string($department)."\n"; $email_message .= "Problem: ".clean_string($problem)."\n"; $email_message .= "Priority: ".clean_string($priority)."\n"."\n"; $email_message .= "Guest Name: ".clean_string($gst_name)."\n"; $email_message .= "Guest Room: ".clean_string($gst_room)."\n"; $email_message .= "Comments: ".clean_string($comments)."\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); ?> Thank you for submitting the alert. Please call RAM to raise awareness as well. the way it looks to me, is that the script somehow stops after the $function died($error) paragraph and displays the rest of the script as plain text. whats going on? Thank you |
Christian J |
Jul 7 2014, 04:39 AM
Post
#4
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
All form fields need NAME attributes, so that their values can be fetched by the PHP script's $_POST variables.
As a sidenote, the HTML has some serious structural errors (esp. the table). |
Armitage2k |
Jul 7 2014, 05:47 AM
Post
#5
|
Group: Members Posts: 4 Joined: 7-July 14 Member No.: 21,206 |
can you be a little more specific? all my form fields actually do have name attributes, so i didnt see an issue there...
I have a few input boxes where the attribute is not NAME but rather ID, does that matter? Other than that, I dont see any issues with the $_POST command. Thanks, A2k |
Christian J |
Jul 7 2014, 08:51 AM
Post
#6
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
|
Frederiek |
Jul 7 2014, 10:13 AM
Post
#7
|
Programming Fanatic Group: Members Posts: 5,146 Joined: 23-August 06 From: Europe Member No.: 9 |
A table consists of rows <tr> containing cells <td>.
Any content can't be put inside TR, it needs to go inside TD cells. See also http://htmlhelp.com/reference/html40/tables/table.html . |
Armitage2k |
Jul 8 2014, 09:39 AM
Post
#8
|
Group: Members Posts: 4 Joined: 7-July 14 Member No.: 21,206 |
OK, I managed to get rid of the errors by fixing my NAME attributes, as well made some adjustments to my table.
The problem I am facing now is that the script will not generate the email. I tried several different email addresses, no result. Error Reporting is set to E_ALL but no message pops up. CODE <html> <head> </head> <body> <?php ini_set('display_errors', 'On'); error_reporting(E_ALL); if(isset($_POST['name'])) { // CHANGE THE TWO LINES BELOW $email_to = "my@email.com"; $email_from = "donotreply@email.com"; $email_subject = "My Subject line"; function died($error) { // your error code can go here echo "<table><tr><img src=\"../../images/iclogo_small.jpg\" style=\"width: 300px\"/></tr><tr><br><strong>ERROR</strong><br />We are very sorry, but there were error(s) found with the form you submitted.</tr>"; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go <a href=\"java script:history.back()\">back</a> and fix these errors.<br /><br />"; die(); } // validation expected data exists if ( !isset($_POST['name']) || !isset($_POST['department']) || !isset($_POST['problem']) || !isset($_POST['priority']) || !isset($_POST['gst_name']) || !isset($_POST['gst_room']) || !isset($_POST['incident']) || !isset($_POST['action']) || !isset($_POST['gst_temp']) || !isset($_POST['gst_history']) ) { died('ERROR - There seems to be a problem with the form you submitted.'); } $name = $_POST['name']; // required $department = $_POST['department']; // required $problem = $_POST['problem']; // required $priority = $_POST['priority']; // not required $gst_name = $_POST['gst_name']; // not required $gst_room = $_POST['gst_room']; // not required $incident = $_POST['incident']; // required $action = $_POST['action']; // required $gst_temp = $_POST['gst_temp']; // required $gst_history = $_POST['gst_history']; // required $date = date("F j, Y, g:i a"); $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; $string_exp = "/^[A-Za-z .'-]+$/"; if (strlen($incident) < 2) { $error_message .= 'The Incident comments you entered do not appear to be valid.<br />'; } if (strlen($action) < 2) { $error_message .= 'The Action Taken comments you entered do not appear to be valid.<br />'; } if (strlen($error_message) > 0) { died($error_message); } $email_message = "Kindly be informed that a Guest Problem Alert has been raised. Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $date .= "Date: ".($date)."\n"; $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Department: ".clean_string($department)."\n"; $email_message .= "Problem: ".clean_string($problem)."\n"; $email_message .= "Priority: ".clean_string($priority)."\n"."\n"; $email_message .= "Guest Name: ".clean_string($gst_name)."\n"; $email_message .= "Guest Room: ".clean_string($gst_room)."\n"; $email_message .= "Incident: ".clean_string($incident)."\n"; $email_message .= "Action: ".clean_string($action)."\n"; $email_message .= "Gst. Manner: ".clean_string($gst_temp)."\n"; $email_message .= "Updated by: ".clean_string($gst_history)."\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); ?> <!-- place your own success html below --> <font>Thank you for submitting the alert. Please call RAM to raise awareness as well.<br /><a href="java script:history.back()">Go back</a></font> <?php } die(); ?> </body> </html> Suggestions anyone? Thanks |
CharlesEF |
Jul 8 2014, 10:17 AM
Post
#9
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Are you testing on a Windows system? Try it out on your web host site. Other than $date not being used in the actual e-mail message I don't see anything that jumps out at me. Try removing the @ character in front of mail. The @ will suppress errors, if I remember correctly.
|
Lo-Fi Version | Time is now: 24th April 2024 - 08:05 PM |