admin管理员组文章数量:1326303
I am trying to insert rows into a table I've set up based on user's answers. I have made a child theme and a custom template for the page with the form. My issue is that I have no way of knowing what's going wrong where and why the data isn't being inserted. my js onclick function is as follows, and this file is called question_submit.js:
jQuery(document).ready(function(){
jQuery("#questionSubmit").click(function(){
alert("clicked");
jQuery.ajax({
type: 'POST',
dataType: 'json',
url: my_ajax_object.ajax_url,
data: {
'action' : 'dbtest',
'option': 1,
},
success: function(data){
alert(data);
}
});
})
});
Here is where I enqueue the script:
function my_enqueue() {
wp_enqueue_script( 'question_submit', get_stylesheet_directory_uri() . '/assets/js/question_submit.js', array('jquery') );
wp_localize_script( 'question_submit', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
And finally here is the function that's binded to the action:
function dbtesting(){
global $wpdb;
$data = $_POST['data'];
//$option = sanitize_text_field($_POST["option"]);
$tableName = 'user_answers';
$insert_row = $wpdb->insert(
$tableName,
array(
'user_id' => 2,
'module_id' => 3,
'section_id' => 1,
'question_id' => 2,
'option_no' => 1,
)
);
// if row inserted in table
if($insert_row){
echo json_encode(array('res'=>true, 'message'=>__('New row has been inserted.')));
}else{
echo json_encode(array('res'=>false, 'message'=>__('Something went wrong. Please try again later.')));
}
wp_die();
}
add_action( 'wp_ajax_dbtest', 'dbtesting' );
add_action( 'wp_ajax_nopriv_dbtest', 'dbtesting' );
Both of these functions are in my child theme's functions.php file. Right now I am using hard-coded values jsut to test the functionality, later these values will be dependent on the form answers.
When I click the submit button, there is no change in my table but also no php errors. There is no evidence of the ajax query going through on the XHR section of networks in development tools.
版权声明:本文标题:jquery - My function containing a mysql query launched by ajax is not working in wordpress. What am I missing? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206451a2432904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论