122lettie Posted January 12, 2011 Share Posted January 12, 2011 Hi, I'm a beginner to all this stuff, but I'm redesigning a website that needs an online booking form. I've made the form in HTML and the PHP bit to send direct to an email address. I found the template online. It works fine at sending to the email. But as soon as I started customising it and adding fields for "Date of Arrival" etc it's stopped working. I get a variety of error messages. Could you possibly take a look at it and see what I'm doing wrong? Thanks HTML (booknow.html) <form name="contactform" method="post" action="send_form_email.php"> <table width="450px"> </tr> <tr> <td valign="top"> <label for="first_name">First Name *</label> </td> <td valign="top"> <input type="text" name="first_name" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top""> <label for="last_name">Last Name *</label> </td> <td valign="top"> <input type="text" name="last_name" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top"> <label for="email">Email Address *</label> </td> <td valign="top"> <input type="text" name="email" maxlength="80" size="30"> </td> </tr> <tr> <td valign="top"> <label for="telephone">Telephone Number *</label> </td> <td valign="top"> <input type="text" name="telephone" maxlength="30" size="30"> </td> </tr> <tr> <td valign="top"> <label for="arrival">Date of Arrival *</label> </td> <td valign="top"> <input type="text" name="arrival" maxlength="30" size="30"> </td> </tr> <tr> <td valign="top"> <label for="comments">Comments</label> </td> <td valign="top"> <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea> </td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="Submit"> <a href="http://www.freecontactform.com/email_form.php"></a> </td> </tr> </table> </form> PHP bit (send_form_email.php) <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "MYEMAIL"; $email_subject = "SUBJECT"; 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['arrival']) !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']; // required $telephone = $_POST['arrival']; // required $comments = $_POST['comments']; // not required $error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!eregi($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!eregi($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(strlen($telephone) < 2) { $error_message .= 'The Telephone you entered does not appear to be valid.<br />'; } if(strlen($arrival) < 2) { $error_message .= 'The Date of Arrival you entered does 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 .= "Date of Arrival: ".clean_string($arrival)."\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); ?> <!-- include your own success html here --> Thank you for contacting us. We will be in touch with you soon. <? } ?> I think something's wrong with the way I added Date of Arrival, but I don't know what. Thanks in advance for any advice Quote Link to comment https://forums.phpfreaks.com/topic/224169-newbie-php-booking-form-what-am-i-doing-wrong/ Share on other sites More sharing options...
dragon_sa Posted January 12, 2011 Share Posted January 12, 2011 Hi you have set the wrong variable name for arrival this $telephone = $_POST['telephone']; // required $telephone = $_POST['arrival']; // required should be $telephone = $_POST['telephone']; // required $arrival = $_POST['arrival']; // required also good practice is this if(isset($_POST['email'])) { should be if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['email'])) { Quote Link to comment https://forums.phpfreaks.com/topic/224169-newbie-php-booking-form-what-am-i-doing-wrong/#findComment-1158329 Share on other sites More sharing options...
122lettie Posted January 12, 2011 Author Share Posted January 12, 2011 Hi you have set the wrong variable name for arrival this $telephone = $_POST['telephone']; // required $telephone = $_POST['arrival']; // required should be $telephone = $_POST['telephone']; // required $arrival = $_POST['arrival']; // required also good practice is this if(isset($_POST['email'])) { should be if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['email'])) { Thanks so much! Works perfect now That was a stupid mistake. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/224169-newbie-php-booking-form-what-am-i-doing-wrong/#findComment-1158333 Share on other sites More sharing options...
cyberRobot Posted January 12, 2011 Share Posted January 12, 2011 @dragon_sa Just curious, why is it good practice to check the request method? Isn't it enough that data was passed through $_POST? if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['email'])) { Quote Link to comment https://forums.phpfreaks.com/topic/224169-newbie-php-booking-form-what-am-i-doing-wrong/#findComment-1158334 Share on other sites More sharing options...
dragon_sa Posted January 12, 2011 Share Posted January 12, 2011 In this instance it was ok, but most of the time people check for the submit button, IE does not support this if the enter key is pressed and thus anycode that uses isset($_POST['submit']) will not detect a posted form. This is why it is good practice to do so, informing 122lettie on this matter is especially important as he is using copied code from the web and does not really understand how it all works yet. Get in the habit and it wont be a problem in the future. Quote Link to comment https://forums.phpfreaks.com/topic/224169-newbie-php-booking-form-what-am-i-doing-wrong/#findComment-1158343 Share on other sites More sharing options...
cyberRobot Posted January 12, 2011 Share Posted January 12, 2011 In this instance it was ok, but most of the time people check for the submit button, IE does not support this if the enter key is pressed O' ya, I remember those days. Thanks for the clarification. Quote Link to comment https://forums.phpfreaks.com/topic/224169-newbie-php-booking-form-what-am-i-doing-wrong/#findComment-1158347 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.