admin管理员组文章数量:1122832
I have seen a number of methods dating back to about 2011 in order to take a form and have its data posted via ajax.jquery. I have tried a couple and am just getting the page to reload despite using preventdefault functions
.
I am trying to take data from my form, have the jQuery listen for the form submit by targeting the actual form id and then call a PHP function in order to have that data posted to the db. Ultimately nothing happens other than a page reload. Since this is a WP page, I know that some of the functions and handling differ from regular webpages. Things like the ajax url, can be passed back to the functions etc. That is why I Am posting this here.
the form:
function add_entry(){
?>
<form action="" method="post" id="ajax-add-to-form" autocomplete="off">
<input type="text" id="FName" name="FName" placeholder="First Name" value="" required>
<input type="text" id="LName" name="LName" placeholder="Last Name" value="" required>
<select id="size" required>
<option value="">Size</option>
<?php for($i=1; $i<=15; $i++){?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
<input type="text" id="MobileNumber" name="MobileNumber" placeholder="Phone Number" value="" required>
<input type="email" id="Customer_Email" name="Customer_Email" placeholder="Email Address" value="" required>
<input type="submit" name="ajax-add-to-form" value="Add to">
<?php
}
add_shortcode('add_entry', 'add_entry');
</form>
the jquery
jQuery('document').ready( function(){
jQuery('#ajax-add-to-form').on('submit', function(e){
e.preventDefault();
jQuery('#jx-loading').show();
jQuery.ajax({
type: 'post',
url: my_ajax.ajax_url,
data: {
action: 'ajax-php-function'},
success: function(data)
{jQuery('#jx-loading').hide();}
})
});
return false;
});
the php
function ajax-php-function() {
global $wpdb;
$table = "mytablename";
$FName= $_POST['FName'];
$LName= $_POST['LName'];
$MobileNumber= $_POST['MobileNumber'];
$Email = $_POST['Email '];
$data = array(
'FName' => $FName,
'LName' => $LName,
'MobileNumber' => $MobileNumber,
'Email' => $Email);
$success = $wpdb->insert( $table, $data );
if($success)
{echo json_encode($last_data);}
else{ echo 'error'; } die;
}
I have seen a number of methods dating back to about 2011 in order to take a form and have its data posted via ajax.jquery. I have tried a couple and am just getting the page to reload despite using preventdefault functions
.
I am trying to take data from my form, have the jQuery listen for the form submit by targeting the actual form id and then call a PHP function in order to have that data posted to the db. Ultimately nothing happens other than a page reload. Since this is a WP page, I know that some of the functions and handling differ from regular webpages. Things like the ajax url, can be passed back to the functions etc. That is why I Am posting this here.
the form:
function add_entry(){
?>
<form action="" method="post" id="ajax-add-to-form" autocomplete="off">
<input type="text" id="FName" name="FName" placeholder="First Name" value="" required>
<input type="text" id="LName" name="LName" placeholder="Last Name" value="" required>
<select id="size" required>
<option value="">Size</option>
<?php for($i=1; $i<=15; $i++){?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
<input type="text" id="MobileNumber" name="MobileNumber" placeholder="Phone Number" value="" required>
<input type="email" id="Customer_Email" name="Customer_Email" placeholder="Email Address" value="" required>
<input type="submit" name="ajax-add-to-form" value="Add to">
<?php
}
add_shortcode('add_entry', 'add_entry');
</form>
the jquery
jQuery('document').ready( function(){
jQuery('#ajax-add-to-form').on('submit', function(e){
e.preventDefault();
jQuery('#jx-loading').show();
jQuery.ajax({
type: 'post',
url: my_ajax.ajax_url,
data: {
action: 'ajax-php-function'},
success: function(data)
{jQuery('#jx-loading').hide();}
})
});
return false;
});
the php
function ajax-php-function() {
global $wpdb;
$table = "mytablename";
$FName= $_POST['FName'];
$LName= $_POST['LName'];
$MobileNumber= $_POST['MobileNumber'];
$Email = $_POST['Email '];
$data = array(
'FName' => $FName,
'LName' => $LName,
'MobileNumber' => $MobileNumber,
'Email' => $Email);
$success = $wpdb->insert( $table, $data );
if($success)
{echo json_encode($last_data);}
else{ echo 'error'; } die;
}
Share
Improve this question
edited Jan 4, 2021 at 22:36
theodore
asked Jan 4, 2021 at 20:41
theodoretheodore
111 silver badge3 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 0Ultimately the form and its old handler were being trashed by some other element in the website. Could have been WP or one of the few plug ins I could not remove as it is the page builder (Elementor, Elementor Essentials). After disabling all the other plug ins it is narrowed down to that. The code solution was: ....
jQuery(document).on('submit', '#ajax-add-to-waitlist-form', function(e){
e.preventDefault();
.......
Instead of
jQuery('#ajax-add-to-form').on('submit', function(e){
e.preventDefault();
本文标签:
版权声明:本文标题:prevent default not stopping page refresh. Passing form information to and from php with ajax in a wordpress site 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283139a1926869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
e.preventDefault();
before anything else on thejQuery('#ajax-add-to-form').on('submit', function(e){
function. Let me know if works. – Celso Bessa Commented Jan 4, 2021 at 21:19