我正在開發一個WordPress插件,並且難以嘗試使用Ajax將數據發佈到單獨的PHP文件而無需重新加載頁面。在後端,PHP文件依靠if(isset())來執行SQL查詢。在客戶端,用戶應該看到單個記錄fadeOut和記錄被成功刪除的消息。JQuery和AJAX發佈到PHP無需重新加載頁面在WordPress中
更新:JavaScript的工作範圍內,一旦表單按鈕被點擊,正確的div淡入和淡出。但是,PHP文件沒有得到任何值,如var_dump所測試的。
HTML代碼:
<!-- problem: when setting "action" to a url, that page is automatically loaded. I want to stay on the existing page. -->
<form action='' id='form-delete-assoc' method='post'>
<input name='remove_all' type='submit' id='remove-all' value='Remove All'></input>
</form>
JQUERY:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('tr.assoc_row').show();
$('#settings-removed-msg').hide();
$('#new-assoc-msg').hide();
$('#formdeleteassoc').submit(function(e){
e.preventDefault();
$.ajax ({
type: 'POST',
url: '<?php echo $cb_t2c_remove_all_url; ?>',
data: {removeall: 'yes'
},
success: function() {
$('#settings-removed-msg').fadeIn('fast');
$('tr.assoc_row').fadeOut('fast');
}
});
});
$('#formsavesettings').submit(function(){
$('#new-assoc-msg').fadeIn('fast');
});
});
</script>
remove_all.php:
<?php
//remove_all.php
global $wpdb;
$prefix = $wpdb->prefix;
$remove_var_dump = $_POST['removeall']; //returning NULL
var_dump($remove_var_dump);
if (isset($_POST['removeall'])){
$wpdb->query("DELETE FROM ".$prefix."cb_tags2cats");
}
?>
嘗試增加回報虛假的;在你的Ajax調用之後。 – akiller 2012-02-18 00:29:43