I'm trying to post a form to a database using ajax. I have tested the form and the php which all work when I manually submit. But the issue I have is when I try to auto-submit on page load using ajax it just doesn't seem to fire.
I don't get any console errors and the form does get removed. It is just the ajax that doesn't kick in.
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';
}
}
?>
</div>