0

HTML 代码

<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="Joebloggs@example.com">

    <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 代码

<?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 = "joebloggs@example.com";
        $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>';
        }
    }
}
?>

好的,所以我上面的代码工作得很好,我有 JS 弹出警报。但是,当我确定 JS 警报时,它会将我带回 mail.php 脚本,而不是它源自的 HTML 页面,我将如何纠正这个问题?

4

3 回答 3

1

这应该有助于解决您当前的问题......但在未来,请尝试 AJAX 调用。

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>';
        }

编辑

忘记包括第一个实例

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>';
    }
于 2013-03-01T16:18:24.430 回答
0

当您提交到 mail.php 时,mail.php 将成为您的用户所在的新页面。如果您希望他们仍然在他们调用它的页面上,您需要通过 AJAX 提交它,或者告诉浏览器返回调用脚本的页面。要以第二种方式执行此操作,请将其添加到您的脚本回显中:

window.history.back()
于 2013-03-01T15:44:28.510 回答
0

这取决于您在 HTML 页面中所做的工作:

  1. 如果您的 PHP 是通过 ajax 调用的,那么您需要添加return false到启动该 ajax 调用的 javascript 函数中。

  2. 如果您有一个表单,并且表单通过常规回发到操作的 url 提交是您处理该 PHP 页面的方式,那么您需要在 PHP 中添加一个重定向回该页面,同时将该警报消息存储在会话中,这样它就不会迷路,或者在 PHP 之后抛​​出一些 HTML。

于 2013-03-01T15:45:10.717 回答