업데이트 2/28/2012 :이 질문에는 @charlietfl 덕분에 해결책이 있습니다.
일부 JQuery 스크립트 내에 양식과 AJAX 호출이 있는데 AJAX가 성공적으로 실행되는 것처럼 보입니다. 그러나 PHP 파일의 $ _POST 변수는 여전히 비어 있습니다. 내가 뭘 잘못하고 있는지 모르겠다. 내 코드는 아래에 설명되어 있습니다.
주요 질문은 PHP 파일과 관련이 있습니다. PHP $ _POST 변수가 'yes'로 설정되지 않은 이유는 무엇입니까? var_dump를 수행하면 일관되게 NULL이 표시됩니다. 그러나 나는 수동으로 'removeall'을 임의의 값으로 설정한다고 믿습니다.이 경우 '예'는 일반적인 AJAX 메소드를 사용합니다. PHP 파일에서 'removeall'이라는 레이블을 가진 $ _POST 변수를 'yes'로 설정해야합니까?
내가하고있는 것이 무엇이든간에 누군가에게 완전히 분명한 희망이 있습니다.
자바 스크립트 :
<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(); //This is working to prevent normal submission of the form.
$.ajax ({
type: 'POST',
url: '<?php echo $cb_t2c_remove_all_url; ?>', //I have checked this to make sure it is the correct url for the PHP file.
data: {removeall: 'yes' //This is the data that is NOT getting passed to the PHP file.
},
success: function() {
$('#settings-removed-msg').fadeIn('fast'); //This gets triggered.
$('tr.assoc_row').fadeOut('fast'); //This gets triggered
}
});
});
});
PHP 코드 :
<?php
//remove_all.php
global $wpdb;
$prefix = $wpdb->prefix;
$remove_var_dump = $_POST['removeall']; //returning NULL
var_dump($remove_var_dump); //returning NULL
if ($_POST['removeall'] == 'yes') {
echo 'This was set to yes, everything is working.';
}
else {
echo 'This was not set to yes, it is still not working.';
}
?>
솔루션 :
/*
JQUERY action processed by AJAX
*/
add_action('init', 'cb_t2c_action_javascript');
function cb_t2c_action_javascript() {
?>
<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(); //Works to prevent normal submission of the form.
var data = {
action: 'cb_t2c_ajax_action',
removeall: 'yes'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.ajax ({
type: 'POST',
url: ajaxurl,
data: data,
success: function() {
$('#settings-removed-msg').fadeIn('fast'); //Working now
$('tr.assoc_row').fadeOut('fast'); //Working now
}
});
$('#formsavesettings').submit(function(){
$('#new-assoc-msg').fadeIn('fast'); //Working now
});
});
});
</script>
<?php
}
//Add the action to process the AJAX.
add_action('wp_ajax_cb_t2c_ajax_action', 'cb_t2c_action_callback');
add_action('wp_ajax_nopriv_cb_t2c_ajax_action', 'cb_t2c_action_callback');
function cb_t2c_action_callback() {
global $wpdb; // this is how you get access to the database
$prefix = $wpdb->prefix;
$remove_var_dump = $_POST['removeall']; //returning NULL
var_dump($remove_var_dump);
$removeall = $_POST['removeall'];
if (isset($removeall)){
$wpdb->query("DELETE FROM wp_cb_tags2cats");
}
die(); // this is required to return a proper result
}
패킷 스니퍼는 무엇이라고 말합니까? –
어떻게 remove_all.php의 outpit을 확인하고 있습니까? –
Somesh, 실제로 에코가 아닌 다른 것을 사용하여 테스트하고 있습니다. 데이터베이스에서 간단한 작업입니다. 예 : '($ _POST ['removeall '] =='예 ') { \t $ wpdb-> query ("DELETE FROM". $ prefix. "cb_tags2cats"); \t} removeall 변수를 'yes'로 설정하면 특정 테이블에서 모든 데이터가 삭제됩니다. 이것은 서버에 게시하는 양식을 사용하는 것으로 작동하지만 다른 이유로 인해 AJAX를 통해 다른 PHP 파일로 보내지는 $ _POST 변수가 필요합니다. – ChrisBuck