简体   繁体   English

表单提交,无法停止刷新

[英]Form submission, just can't stop refresh

So I am very new to this stuff and i have a specific problem I just can't fix. 所以我对这些东西很陌生,我有一个我无法解决的特定问题。 I have searched all over, and I have tried loads of solutions to no avail. 我搜索了所有内容,并且尝试了很多解决方案,但均无济于事。 I'm sure I'm missing something or have something in the wrong order but I just need some guidance. 我确定我缺少某些东西或顺序错误,但是我只需要一些指导。

I have a simple form in my website and I can't stop it refreshing the page on submit. 我的网站上有一个简单的表格,我无法阻止它刷新提交页面。 There's some php validating happening also. 还有一些php验证正在发生。

Here's a link to the website: www.nathanchapmanphotography.co.uk 这是网站链接: www.nathanchapmanphotography.co.uk

Any help would be massively appreciated. 任何帮助将不胜感激。

 $("form").submit(function() { var error = ""; var success = ""; var fail = ""; if ($("#name").val() == "") { error += "name required<br>"; } if ($("#email").val() == "") { error += "email required<br>"; } if ($("#message").val() == "") { error += "message required<br>"; } if (error != "") { $("#error").html(error); $("#success").html(success); $("#fail").html(fail); return false; } else { sendContactForm(); return false; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post"> <p class="form-p">name: <input class="input" type="text" id="name" name="name"> </p> <p class="form-p">email: <input class="input" type="email" id="email" name="email"> </p> <p class="form-p">message: <br/> <textarea id="message" cols="40" rows="7" name="message"></textarea> </p> <button type="submit" id="submit">submit</button> <div id="error"> <? echo $error; ?> </div> <div id="success"> <? echo $success; ?> </div> <div id="fail"> <? echo $fail; ?> </div> </form> 

You need to prevent the default action of the submit event: 您需要阻止Submit事件的默认操作:

$("form").submit(function(event) { // capture the function's event here
    event.preventDefault(); // use the captured event here
    // rest of your code...

EDIT: from the OP's website after the change - 编辑:更改后从OP的网站-

在此处输入图片说明

I'm not 100% sure what you are looking for,..however, 我不是100%知道您要寻找的东西..但是

If sendContactForm does the posting of the form data and you just don't want the submit to happen automatically you can make the button of type button and bind to it for the validation. 如果sendContactForm进行了表单数据的投递,而您只是不希望提交自动发生,则可以使按钮类型为button并绑定到该button以进行验证。

 $(document).ready(function() { function sendContactForm(){ console.log('sendContactForm'); }; $('#submit').on('click', function() { var error = ""; var success = ""; var fail = ""; if ($("#name").val() === "") { error += "name required<br>"; } if ($("#email").val() === "") { error += "email required<br>"; } if ($("#message").val() === "") { error += "message required<br>"; } if (error !== "") { $("#error").html(error); $("#success").html(success); $("#fail").html(fail); } else { sendContactForm(); } }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post"> <p class="form-p">name: <input class="input" type="text" id="name" name="name"> </p> <p class="form-p">email: <input class="input" type="email" id="email" name="email"> </p> <p class="form-p">message: <br/> <textarea id="message" cols="40" rows="7" name="message"></textarea> </p> <button type="button" id="submit">submit</button> <div id="error"> <? echo $error; ?> </div> <div id="success"> <? echo $success; ?> </div> <div id="fail"> <? echo $fail; ?> </div> </form> 


or,..if you want to only have more control over the submit itself without the page refreshing, you can use ajax. 或者,..如果您只希望对提交本身进行更多控制而不刷新页面,则可以使用ajax。 Similar to this: 与此类似:

var $form = $("form");

$.ajax({
    data: $form.serialize(),
    url: $form[0].action,
    type: 'post',
    dataType: 'json',
    success: function(data) {
        sendContactForm();
    }
});

 $(document).ready(function() { function postData() { var $form = $("form"); $.ajax({ data: $form.serialize(), url: $form[0].action, type: 'post', dataType: 'json', success: function(data) { sendContactForm(); } }); } function sendContactForm() { console.log('sendContactForm'); } $('#submit').on('click', function() { var error = ""; var success = ""; var fail = ""; if ($("#name").val() === "") { error += "name required<br>"; } if ($("#email").val() === "") { error += "email required<br>"; } if ($("#message").val() === "") { error += "message required<br>"; } if (error !== "") { $("#error").html(error); $("#success").html(success); $("#fail").html(fail); } else { postData(); } }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" action="http://yourtageturl"> <p class="form-p">name: <input class="input" type="text" id="name" name="name"> </p> <p class="form-p">email: <input class="input" type="email" id="email" name="email"> </p> <p class="form-p">message: <br/> <textarea id="message" cols="40" rows="7" name="message"></textarea> </p> <button type="button" id="submit">submit</button> <div id="error"> <? echo $error; ?> </div> <div id="success"> <? echo $success; ?> </div> <div id="fail"> <? echo $fail; ?> </div> </form> 

So I managed to sort it using both your suggestions, thanks so much. 因此,我设法根据您的建议对它进行了排序,非常感谢。 The issue was that the function I found online to send the data actually wasn't doing anything. 问题是我在网上找到的用于发送数据的功能实际上没有执行任何操作。 I have it running perfectly now, no server side validation but that's for another day. 我现在可以完美运行,无需服务器端验证,但这又是一天。 If your interested, here's the revised code. 如果您有兴趣,这里是修改后的代码。

 $("form").submit(function(e) { e.preventDefault(); var error = ""; var success = "Thank you for your message!<br/>I'll get back to you shortly." var url = "index.php"; // the script where you handle the form input. if ($("#name").val() == "") { error += "name required<br>"; } if ($("#email").val() == "") { error += "email required<br>"; } if ($("#message").val() == "") { error += "message required<br>"; } if (error != "") { $("#error").html(error); //alert("We need your" + error + "please!" ); } else { $.ajax({ type: "POST", url: url, data: $("form").serialize(), // serializes the form's elements. success: function(data) { $("#success").html(success); $("#error").html(error); } }); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post"> <p class="form-p">name: <input class="input" type="text" id="name" name="name"> </p> <p class="form-p">email: <input class="input" type="email" id="email" name="email"> </p> <p class="form-p">message: <br/> <textarea id="message" cols="40" rows="7" name="message"></textarea> </p> <button type="submit" id="submit">submit</button> <div id="error"> <? echo $error; ?> </div> <div id="success"> <? echo $success; ?> </div> <div id="fail"> <? echo $fail; ?> </div> </form> 

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

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