简体   繁体   English

PHP电子邮件表单,响应当前的HTML而不是打开“谢谢”页面

[英]PHP email form that responds to current html rather than opening a 'thank you' page

I've set up an email form on my website and right now it sends users to a thank you page using header('Location:emailConfirmation.html'); 我在我的网站上设置了一个电子邮件表单,现在它使用header('Location:emailConfirmation.html');将用户发送到感谢页面header('Location:emailConfirmation.html'); . I want to try something else and replace the html of the form with a message using javascript. 我想尝试别的东西,并使用javascript将消息替换为表单的html。 ie call a function like this 即调用这样的函数

function emailConfirmed()
{
    $('#email').load('emailConfirmation.html');
}

Something less intrusive, that doesn't leave the main page. 不那么干扰的东西,不会离开主页面。 Is something like this possible? 这样的事情可能吗?

Here's my current form code. 这是我目前的表单代码。

<form method="POST" name="contactform" action="scripts/contact-form-handler.php">
    <p>
        <label for='name'>Your Name:</label> <br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Email Address:</label> <br>
        <input type="text" name="email"> <br>
    </p>
    <p>
        <label for='message'>Message:</label> <br>
        <textarea name="message" maxlength="600" cols="25" rows="6" style="margin: 2px; width: 300px; height: 110px;resize:none;"></textarea>
    </p>
    <input type="submit" value="Submit"><br>
</form>

<script language="JavaScript">
    var frmvalidator  = new Validator("contactform");
    frmvalidator.addValidation("name","req","Please provide your name");
    frmvalidator.addValidation("email","req","Please provide your email");
    frmvalidator.addValidation("email","email","Please enter a valid email address");
</script>

Then you should use $.ajax() to send mail like, 然后你应该使用$ .ajax() send mail如,

SCRIPT 脚本

$(function(){
    $('#submit').on('click',function(){
        $.ajax({
           url:$('form[name="contactform"]').attr('action'),
           data:$('form[name="contactform"]').serialize(),
           type:'POST',
           success:function(){
              $('#email').load('emailConfirmation.html');
           }
        });
    });
});

Add an id to your submit button like, submit button添加id ,例如

<input type="submit" value="Submit" id="submit"><br>

Also to get the the message in #email add a div after your form like, 也得到了message#email添加一个div你以后form一样,

<form ...>
   ....
</form>
<div id="email"></div><!-- Element to get the message afte ajax success callback -->

If the main part of the page is within a div (say, "main") then you can do something like this: 如果页面的主要部分位于div(例如“main”)中,那么您可以执行以下操作:

$('#main').html("Thank you.<br/><br/>Your form has been sent.");

If you'd like to include more content, you can load it from an external file or even just hard-code it into the JavaScript. 如果您想要包含更多内容,可以从外部文件加载它,甚至只需将其硬编码到JavaScript中。

you want to load html file. 你想加载HTML文件。 done using this code i hope this is working for you. 使用此代码完成,我希望这对你有用。

$( "#email" ).load( "emailConfirmation.html", function() {
         alert( "Load was performed." );
});

please try and More Detail 请尝试更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM