Jump to content

Newbie - PHP Booking Form - What am I doing wrong?


122lettie

Recommended Posts

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 :)

Link to comment
Share on other sites

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'])) {

Link to comment
Share on other sites

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 :D That was a stupid mistake. Thank you

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.