[英]Ajax post form auto submit on load
I'm trying to post a form to a database using ajax. 我正在尝试使用ajax将表单发布到数据库。 I have tested the form and the php which all work when I manually submit. 我已经测试了表单和php,这些表单和php在我手动提交时都可以正常工作。 But the issue I have is when I try to auto-submit on page load using ajax it just doesn't seem to fire. 但是我遇到的问题是,当我尝试使用ajax自动提交页面加载时,它似乎没有触发。
I don't get any console errors and the form does get removed. 我没有任何控制台错误,并且确实删除了该表格。 It is just the ajax that doesn't kick in. 只是没有加入的ajax。
setTimeout(function() { $("form.display-hide").submit(function(e) { e.preventDefault(); $.ajax({ url: '//www.mysite.com/inc/page-trustpilot.php', type: "POST", data: $(this).serialize(), success: function() { alert('hello'); } }); // AJAX Get Jquery statment }); $("form.display-hide").remove(); }, 2000);
<form class="display-hide" method="post"> <input class="totaltrsut" type="text" value="" name="totaltrsut"> <input class="totalreviews" type="number" value="" name="totalreviews"> <input type="hidden" name="token" value="<?php echo $newToken; ?>"> <input class="committodb" type="submit" value="Add Stats"> </form> <?php if (!empty($_POST)) { global $wpdb; $successa=$wpdb->update( '6H921_dc_additional', array( 'addi_value' => $_POST['totaltrsut'] ), array( 'addi_id' => 1 ), array( '%s', '%d' ), array( '%d' ) ); $successb=$wpdb->update( '6H921_dc_additional', array( 'addi_value' => $_POST['totalreviews'] ), array( 'addi_id' => 2 ), array( '%s', '%d' ), array( '%d' ) ); if($successa && $successb){ //echo 'data has been saved'; } else { //echo 'data has not been saved'; } } ?>
it seems that you removing the form, before you submit it: 似乎您在提交表单之前已删除该表单:
setTimeout(function() {
//registering submit handler
$("form.display-hide").submit(function(
//some code
});
//removing the form immediatly
$("form.display-hide").remove();
}, 2000);
Maybe you want to submit the form before removing it: 也许您想在删除表单之前提交表单:
setTimeout(function() {
//registering submit handler
$("form.display-hide").submit(function(
//some code
});
//submit the the form, which would invoke the submit handler
$("form.display-hide").submit();
//removing the form immediatly
$("form.display-hide").remove();
}, 2000);
You need to trigger the submit
listener on your form. 您需要在表单上触发submit
侦听器。 Give the following a try 尝试以下
setTimeout(submitForm, 2000);
function submitForm(){
$("form.display-hide").submit(function(e) {
e.preventDefault();
$.ajax({
url: '//www.mysite.com/inc/page-trustpilot.php',
type: "POST",
data: $(this).serialize(),
success: function() {
alert('hello');
},
complete: function(){ $("form.display-hide").remove(); }
}); // AJAX Get Jquery statment
});
$("form.display-hide").trigger('submit');
}
This: 这个:
$("form.display-hide").submit(function(e) {
e.preventDefault();
$.ajax({
url: '//www.mysite.com/inc/page-trustpilot.php',
type: "POST",
data: $(this).serialize(),
success: function() {
alert('hello');
}
}); // AJAX Get Jquery statment
will not submit the form. 将不会提交表格。 $().submit()
is an event handler, it does not trigger form submission. $().submit()
是事件处理程序,它不会触发表单提交。
Use .trigger()
to cause the form to submit and register the handler for the submit event before calling setTimeout
like this: 在调用setTimeout
之前,使用.trigger()
使表单提交并注册.trigger()
事件的处理程序,如下所示:
$("form.display-hide").submit(function(e) {
e.preventDefault();
$.ajax({
url: '//www.mysite.com/inc/page-trustpilot.php',
type: "POST",
data: $(this).serialize(),
success: function() {
alert('hello');
}
}); // AJAX Get Jquery statment
Now that you've registered the event handler you can go on and call setTimeout
现在您已经注册了事件处理程序,可以继续并调用setTimeout
setTimeout(function(){
$('form.display-hide').trigger();
}, 2000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.