我尝試通過ajax更新資料,但是没有用.我做錯了什麼? 這是jquery ajax代碼
functions.php中的php代碼
function update_records(){
global $wpdb;
echo $id = $_POST['update_record'];
$db_updated = $wpdb->update( $wpdb->prefix.'contact_form',
array('names' => $_POST['update_name'],
'emails' => $_POST['update_email'],
'gender' => $_POST['update_gender'],
'age' => $_POST['update_age']), array( 'ID' => $id ) );
}
add_action( "wp_ajax_update_records", "update_records" );
add_action( "wp_ajax_nopriv_update_records", "update_records" );
jquery ajax代碼
jQuery('.upd_btn').click(function(){
var id = jQuery(this).attr('data-id');
var name = jQuery(this).attr('data-name');
var email = jQuery(this).attr('data-email');
var gender = jQuery(this).attr('data-gender');
var age = jQuery(this).attr('data-age');
alert(age);
$.ajax({
url: '<?php echo admin_url('admin-ajax.php');?>',
type: 'POST',
data:{
action: 'display_func',
update_record:id,
// update_name:name,
// update_email:email,
// update_gender:gender,
// update_age:age,
},
success: function( data ){
// alert("Records are successfully update");
location.reload();
}
});
});
ajax錯誤螢幕截圖-https://prnt.sc/wdon1x
最新回復
- 6月前1 #
- 6月前2 #
在插入或更新到資料庫之前,請先清理資料,以防止攻击者阅讀文件:
echo
function update_records(){ global $wpdb; $id = $_POST['update_record']; $db_updated = $wpdb->update( $wpdb->prefix.'contact_form', array( 'names' => $_POST['update_name'], 'emails' => $_POST['update_email'], 'gender' => $_POST['update_gender'], 'age' => $_POST['update_age'] ), array( 'ID' => $id ) ); } add_action( "wp_ajax_update_records", "update_records" ); add_action( "wp_ajax_nopriv_update_records", "update_records" );
相似問題
- wordpress:載入更多帖子按钮-AJAXwordpresswpquerywordpressajaxwordpressjquerywordpresssecurity2020-05-11 04:25
- wordpress:如何在wordPress中使用AJAX請求更改PHP變數wordpressphpwordpressthemedevelopmentwordpressajaxwordpressjquery2020-08-25 04:54
- wordpress:JS在正常使用但不在wordpress中工作時wordpresspluginswordpressshortcodewordpressajaxwordpressjavascriptwordpressjquery2020-07-27 01:24
- wordpress:使用JS定位單頁wordpressphpwordpressajaxwordpressjquery2020-07-24 00:53
- wordpress:如何使用Ajax提交日期?wordpresscustomposttypeswordpressajaxwordpressjquerywordpressforms2020-07-21 15:25
首先,在您的jquery事件處理程式中,
action
引數的值應為wp_ajax_
之後的字元串 和wp_ajax_nopriv_
.因此,在您的示例中, 應该是action
.太棒了 是錯誤的。然後在您的php代碼的第3行中,
update_records
關鍵字應删除。因此,您的php代碼應如下所示: