Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Question Contact Form Success Message on contact form not in new window?

    I've followed a tutorial and created a contact form. Once the form has been submitted the browser opens a new page displaying a success message. How do I display the message at the bottom of the contact form itself? I think I need to declare a variable in php then call the variable in the HTML but don't know how to do it. I want to keep the php and html as separate pages.

    PHP Code:
    <?php
    if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to "email@email.com";
    $email_subject "Express Travel Booking";
     
    $Name $_POST['Name']; // required
    $email_from $_POST['email']; // required
    $Telephone $_POST['Telephone']; // not required
    $PickUp $_POST['PickUp']; // required
    $DropOff $_POST['DropOff']; // required
    $Passengers $_POST['Passengers']; // required
    $Date $_POST['Date']; // required
    $Time $_POST['Time']; // required
    $Comments $_POST['Comments']; // required
     
    $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 .= "Name : ".clean_string($Name)."\n";
    $email_message .= "email address : ".clean_string($email_from)."\n";
    $email_message .= "Telephone : ".clean_string($Telephone)."\n";
    $email_message .= "Pick Up : ".clean_string($PickUp)."\n";
    $email_message .= "Drop Off : ".clean_string($DropOff)."\n";
    $email_message .= "Passengers : ".clean_string($Passengers)."\n";
    $email_message .= "Date : ".clean_string($Date)."\n";
    $email_message .= "Time : ".clean_string($Time)."\n\n";
    $email_message .= "Comments : \n\n".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 your enquiry. We will be in touch soon.
     
    <?php
    }
    ?>
    Code:
    <form method="post" action="sendmail.php">
    
    <fieldset>
    <legend>Contact Form</legend>
    
    <div class="Form">
    <label for="Name">Name</label><input type="text" class="inset" name="Name" id="Name" accesskey="1" title="enter name" />
    <label for="email">email address</label><input type="text" class="inset" name="email" id="email" accesskey="2" title="enter email address" />
    <label for="Telephone">Telephone</label><input type="text" class="inset" name="Telephone" id="Telephone" accesskey="3" title="enter Telephone Number" />
    <label for="PickUp">Pick Up</label><input type="text" class="inset" name="PickUp" id="PickUp" accesskey="4" title="enter Pick Up Destination" />
    <label for="DropOff">Drop Off</label><input type="text" class="inset" name="DropOff" id="DropOff" accesskey="5" title="enter Drop Off Destination" />
    <label for="Passengers">Passengers</label><input type="text" class="inset" name="Passengers" id="Passengers" accesskey="5" title="enter number of Passengers" />
    <label for="Date">Date</label><input type="text" class="inset" name="Date" id="Date" accesskey="6" title="enter Date" />
    <label for="Time">Time</label><input type="text" class="inset" name="Time" id="Time" accesskey="7" title="enter Time" />
    <label for="Comments">Comments</label>
    <textarea id="Comments" name="Comments" class="comment"></textarea>
    
    <div class="submit"><input name="Submit" type="submit" class="postc" value="Send"></div>
    <div class="submit"><input name="Reset" type="reset" class="reset" value="Reset"/></div>
    </div>
    </fieldset>
    </form>

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    To keep the PHP and HTML pages separate is easy. Just use the require_once or include_once PHP function and convert the file to a .php file. It will be one script, but different files.

    Sendmail.php

    PHP Code:
    <?php
    if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to "email@email.com";
    $email_subject "Express Travel Booking";
     
    $Name $_POST['Name']; // required
    $email_from $_POST['email']; // required
    $Telephone $_POST['Telephone']; // not required
    $PickUp $_POST['PickUp']; // required
    $DropOff $_POST['DropOff']; // required
    $Passengers $_POST['Passengers']; // required
    $Date $_POST['Date']; // required
    $Time $_POST['Time']; // required
    $Comments $_POST['Comments']; // required
     
    $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 .= "Name : ".clean_string($Name)."\n";
    $email_message .= "email address : ".clean_string($email_from)."\n";
    $email_message .= "Telephone : ".clean_string($Telephone)."\n";
    $email_message .= "Pick Up : ".clean_string($PickUp)."\n";
    $email_message .= "Drop Off : ".clean_string($DropOff)."\n";
    $email_message .= "Passengers : ".clean_string($Passengers)."\n";
    $email_message .= "Date : ".clean_string($Date)."\n";
    $email_message .= "Time : ".clean_string($Time)."\n\n";
    $email_message .= "Comments : \n\n".clean_string($Comments)."\n";
    // create email headers
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    if(@
    mail($email_to$email_subject$email_message$headers))
    {
    //if email sending is successful
    //Note the below message variable. It will be echoed at the bottom of the form
    $complete_message 'Thank you for your enquiry. We will be in touch soon.';
     }
    }
    ?>
    So, form.php will be :

    PHP Code:
    <?php

    require_once('sendmail.php');
    //Same as copy-pasting sendmail.php code at this point

    ?>
    <form method="post">

    <fieldset>
    <legend>Contact Form</legend>

    <div class="Form">
    <label for="Name">Name</label><input type="text" class="inset" name="Name" id="Name" accesskey="1" title="enter name" />
    <label for="email">email address</label><input type="text" class="inset" name="email" id="email" accesskey="2" title="enter email address" />
    <label for="Telephone">Telephone</label><input type="text" class="inset" name="Telephone" id="Telephone" accesskey="3" title="enter Telephone Number" />
    <label for="PickUp">Pick Up</label><input type="text" class="inset" name="PickUp" id="PickUp" accesskey="4" title="enter Pick Up Destination" />
    <label for="DropOff">Drop Off</label><input type="text" class="inset" name="DropOff" id="DropOff" accesskey="5" title="enter Drop Off Destination" />
    <label for="Passengers">Passengers</label><input type="text" class="inset" name="Passengers" id="Passengers" accesskey="5" title="enter number of Passengers" />
    <label for="Date">Date</label><input type="text" class="inset" name="Date" id="Date" accesskey="6" title="enter Date" />
    <label for="Time">Time</label><input type="text" class="inset" name="Time" id="Time" accesskey="7" title="enter Time" />
    <label for="Comments">Comments</label>
    <textarea id="Comments" name="Comments" class="comment"></textarea>

    <div class="submit"><input name="Submit" type="submit" class="postc" value="Send"></div>
    <div class="submit"><input name="Reset" type="reset" class="reset" value="Reset"/></div>
    </div>
    </fieldset>
    </form>
    <?php
    echo $complete_message//The thank you message will be echoed if the emailing is successful in sendmail.php
    ?>
    By leaving out the form action attribute completely, the form will be submitted to the same page where it will be processed by the sendmail.php code.
    Last edited by Redcoder; 10-14-2012 at 10:42 PM.

  • Users who have thanked Redcoder for this post:

    Mehdi72 (10-15-2012)

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks for the reply. Sorted!
    Last edited by Mehdi72; 10-14-2012 at 11:30 PM.

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Change the action of the form from:

    Code:
    <form method="post" action="sendmail.php">
    To:

    PHP Code:
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Or:

    PHP Code:
    <form method="post" action ="<?= $_SERVER['REQUEST_URI']; ?>" >
    Or:

    PHP Code:
    <form method="post" action=""
    The above different code types will make it refer to this script(same script).

    Correcting my earlier comment. Do not delete the action attribute. It is a mandatory attribute.

    You should move the require_once to the top of the PHP script to prevent confusion in case you expand your script later.
    Last edited by Redcoder; 10-14-2012 at 11:44 PM.

  • Users who have thanked Redcoder for this post:

    Mehdi72 (10-15-2012)

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Location
    England
    Posts
    132
    Thanks
    24
    Thanked 0 Times in 0 Posts
    I've put the action attribute back in although the form was sending data without it. It works and validates! I have another unrelated php issue but will post another thread.

    Thanks for the help. If you need a HTML5 or CSS3 template or any help with SEO let me know.
    Last edited by Mehdi72; 10-15-2012 at 02:37 AM.

  • #6
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    333
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Mehdi72 View Post
    I've put the action attribute back in although the form was sending data without it. It works and validates! I have another unrelated php issue but will post another thread.

    Thanks for the help. If you need a HTML5 or CSS3 template or any help with SEO let me know.
    Sure. Glad I could help.



  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •