how update data through ajax and jquery
Question
I try to update data through ajax, but it not worked. What I did wrong?
Here is jquery ajax code
jQuery('.upd_btn').click(function(){
// var id = jQuery(this).attr('data-id');
var form = "#test-form";
// alert(id);
$.ajax({
url: '<?php echo admin_url('admin-ajax.php');?>', // this is the object instantiated in wp_localize_script function
type: 'POST',
data: jQuery(form).serialize(),
// data:{
action: 'update_records', // this is the function in your functions.php that will be triggered
// // update_record : id,
// },
success: function( data ){
alert("Records are successfully update");
location.reload();
}
});
});
this is update query in functions.php
function update_records(){
global $wpdb;
echo $id = $_POST['update_record'];
// echo'<pre>';print_r($_POST);echo'</pre>';
$db_updated = $wpdb->update( $wpdb->prefix.'contact_form',
$data =array(
'names' => $_POST['yourname'],
'emails' => $_POST['customer_email'],
'gender' => $_POST['customer_gender'],
'age' => $_POST['customer_age']
),
array( 'ID' => $id ) );
}
add_action( "wp_ajax_update_records", "update_records" );
add_action( "wp_ajax_nopriv_update_records", "update_records" );
0
2 years
2020-12-31T07:11:17-05:00
2020-12-31T07:11:17-05:00 0 Answers
3 views
0
Leave an answer