0

HTML Code

<div id="fourmstyle" class="fourm">

<form action="scripts/mail.php" method="post">

    <label for="name">Your Name <required>*</required>
        </label>
    <input type="text" name="Name" id="Name" placeholder="Joe Bloggs">

    <label for="email">Your Email <required>*</required>
        </label>
    <input type="text" name="Email" id="Email" placeholder="[email protected]">

    <label for="telephone">Telephone
        </label>
    <input type="text" name="Telephone" id="Telephone">

    <label for="type">Type
        </label>
        <select name="Type">                
            <option value="Booking" selected>Booking</option>
            <option value="B&B">B&amp;B</option>
            <option value="Question">Question</option>
            <option value="General">General</option>
            <option value="Website Feedback">Website Feedback</option>
        </select></p>

         <label for="messsage">Message <required>*</required>
            </label>
         <textarea name="Message" id="Message" rows="5" cols="25">
         </textarea></p>

         <label for="btn">&nbsp;</label>
         <button type="submit" class="button">Submit
         </button>
         <br>&nbsp;<requireddescription> *(indicates that the information is required)
         </requireddescription>
</form>

PHP Code

<?php
if(isset($_POST)) 
{
    $name = (isset($_POST['Name'])) ? strip_tags($_POST['Name']) : NULL; //if name is set, strip html tags, and return it, otherwise set the string as NULL.
    $email = (isset($_POST['Email'])) ? strip_tags($_POST['Email']) : NULL; //same as above.
    $telephone = (isset($_POST['Telephone'])) ? preg_replace('~[^0-9\-]~','',$_POST['Telephone']) : NULL; //if telephone is set, remove any characters that are not a number, or a dash, othewise set as NULL.
    $type = (isset($_POST['Type'])) ? strip_tags($_POST['Type']) : NULL; //strip tags.
    $message = (isset($_POST['Message'])) ? strip_tags($_POST['Message']) : NULL; //strip tags.
    if(empty($name) || empty($email) || empty($message)) 
    { 
        //name, email, and message are required fields, if they are empty, tell the user to go back and fill them in.
        echo '<script type="text/javascript"> alert ("Please go back and fill in all required lines"); </script>';
    }
    else 
    { 
        //if the fields are NOT empty, proceed with the mailing.
        $formcontent=" From: $name \n Type: $type \n\n Message: $message \n\n Telephone: $telephone";
        $recipient = "[email protected]";
        $subject = "Website Contact Form: $type";
        $mailheader = "From: $email \r\n";  

        if(mail($recipient, $subject, $formcontent, $mailheader)) 
        { 
            //if mail is sent to the SMTP server successfully, echo 'thank you'.
            echo '<script type="text/javascript"> alert ("Thankyou '.$name.' we have submitted your message and we will get back to you as soon as possible, if you need to speak to us in the mean time please call 01983 872244 "); </script>';
        }
        else 
        { 
            //otherwise, tell the user it did not go through.
            echo '<script type="text/javascript"> alert ("I am sorry but there has been an error submitting your request please try again or call us on 01983 872244"); </script>';
        }
    }
}
?>

Ok so my code above works quite nicely and I have JS pop up alerts. However, when I ok the JS alert it takes me back to the mail.php script and not the HTML page in which it originated from how would I rectify this ?

5
  • If you just want to go back once, try appending window.history.back() after your alert code.
    – Raad
    Mar 1, 2013 at 15:42
  • attach return false; at the end of alert();
    – zarkoz
    Mar 1, 2013 at 15:42
  • Use AJAX... your user will never leave the page... and you should be handling the alerts in the same js script that calls the AJAX, not the PHP file.
    – zgr024
    Mar 1, 2013 at 16:07
  • @zgr024 I really would love to but I don't know how thats the problem hence why I was searching for a quick fox to the issue ... Mar 1, 2013 at 16:11
  • 1
    I understand you're looking for a quick fix, but sometimes that is not the best approach. An AJAX call is actually quite simple and would solve your problem in a more efficient way than loading the page and using history.back() to then reload the original.
    – zgr024
    Mar 1, 2013 at 16:14

3 Answers 3

1

This should help solve your immediate issue... but in the future, try out an AJAX call.

if(mail($recipient, $subject, $formcontent, $mailheader)) 
        { 
            //if mail is sent to the SMTP server successfully, echo 'thank you' and return to previous page.
            echo '<script type="text/javascript"> alert ("Thankyou '.$name.' we have submitted your message and we will get back to you as soon as possible, if you need to speak to us in the mean time please call 01983 872244 "); window.history.back(); </script>';
        }
        else 
        { 
            //otherwise, tell the user it did not go through.
            echo '<script type="text/javascript"> alert ("I am sorry but there has been an error submitting your request please try again or call us on 01983 872244"); window.history.back(); </script>';
        }

EDIT

forgot to include the first instance

if(empty($name) || empty($email) || empty($message)) 
    { 
        //name, email, and message are required fields, if they are empty, tell the user to go back and fill them in and send them back.
        echo '<script type="text/javascript"> alert ("Please go back and fill in all required lines"); window.history.back()</script>';
    }
0

When you submit to mail.php, mail.php becomes the new page your user is on. If you want them to still be on the page from which they called it, you either need to submit it via AJAX, or tell the browser to go back to the page from which the script was called. To do it the second way, add this to your script echos:

window.history.back()
2
  • although I am sure that AJAX is a much better solution without doubt I am only just begging to venture into the world of programming and don't feel quite up for it yet so could you just show me how you would add these to the echo as I've been trying and struggling ... Mar 1, 2013 at 16:05
  • echo '<script type="text/javascript"> alert ("Thankyou '.$name.' we have submitted your message and we will get back to you as soon as possible, if you need to speak to us in the mean time please call 01983 872244 "); window.history.back();</script>';
    – DiMono
    Mar 1, 2013 at 16:19
0

It depends on what you've done in your HTML page:

  1. If your PHP was called via ajax, then you need to add return false to the javascript function that initiated that ajax call.

  2. If you have a form and the form's submission via regular postback to the action's url was how you processed that PHP page, then you need to either add a redirect in PHP back to that page WHILE storing that alert message in session so it doesn't get lost, OR throw some HTML after that PHP.

3
  • you couldn't show me what you mean by this ? "add a redirect in PHP back to that page WHILE storing that alert message in session so it doesn't get lost" ? Mar 1, 2013 at 16:06
  • or, you could show us your html and javascript so that way I didn't have to guess.
    – Kristian
    Mar 1, 2013 at 16:18
  • I added the HTML code above the JS code is literally just that single few lines embedded within the mail.php document and I think the CSS styling is irrelevant to this problem hope this is ok and thanks for your help... Mar 1, 2013 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.