如何查看存儲在$response
中的數組結構?我試過print_r()
,但我無法在Chrome開發者中看到任何東西(獲得了php控制檯)。我怎樣才能看到這個數組的結構?
function myajax_inputtitleSubmit_func() {
global $wpdb;
$mp_country_table_info = $wpdb->prefix . 'countries '; //Good practice
$mpcountrys = $wpdb->get_var("SELECT labour_cost FROM $mp_country_table_info WHERE id='2' ");
$nonce = $_POST['nextNonce'];
if (! wp_verify_nonce($nonce, 'myajax-next-nonce'))
die ('Busted!');
$numwelds = isset($_POST['numberofwelds']) ? $_POST['numberofwelds'] : '';
$numconwelds = isset($_POST['numberofconwelds']) ? $_POST['numberofconwelds'] : '';
$fortot2 = 5;
$fortot3 = 2;
if (is_numeric($numwelds) && is_numeric($numconwelds))
{
$total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
$total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
$total['tot3'] = ($numwelds + $numconwelds) + $fortot2/$fortot3;
$response = json_encode($total);
print_r($total); // added line
header("Content-Type: application/json");
echo $response;
exit;
}
UPDATE我已經試過以下&它不工作:
$total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
$total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
$total['tot3'] = ($numwelds + $numconwelds) + $fortot2/$fortot3;
$response = json_encode($total);
header("Content-Type: application/json");
echo $response;
exit;
//added bit
echo '<pre>';
echo json_encode($response, JSON_PRETTY_PRINT);
echo '</pre>';
更新兩個這似乎並不工作之一:
$total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
$total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
$total['tot3'] = ($numwelds + $numconwelds) + $fortot2/$fortot3;
echo '<pre>';
echo json_encode($response, JSON_PRETTY_PRINT);
echo '</pre>';
header("Content-Type: application/json");
echo $response;
exit;
這是我第一次嘗試使用chrome php控制檯。我已'添加'它。我也通過查看這個網站http://php-console.com/instance/examples/#debug_vars確保我的工作正常。
更新3 - 所有的全功能
function myajax_inputtitleSubmit_func() {
global $wpdb;
$mp_country_table_info = $wpdb->prefix . 'countries '; //Good practice
$mpcountrys = $wpdb->get_var("SELECT labour_cost FROM $mp_country_table_info WHERE id='2' ");
$nonce = $_POST['nextNonce'];
if (! wp_verify_nonce($nonce, 'myajax-next-nonce'))
die ('Busted!');
$numwelds = isset($_POST['numberofwelds']) ? $_POST['numberofwelds'] : '';
$numconwelds = isset($_POST['numberofconwelds']) ? $_POST['numberofconwelds'] : '';
$fortot2 = 5;
$fortot3 = 2;
if (is_numeric($numwelds) && is_numeric($numconwelds))
{
$total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
$total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
$total['tot3'] = ($numwelds + $numconwelds) + $fortot2/$fortot3;
echo '<pre>';
echo json_encode($total, JSON_PRETTY_PRINT);
echo '</pre>';
header("Content-Type: application/json");
echo $response;
// exit;
}
}
更新4 - 嘗試的var_dump - 也沒有工作
$total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
$total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
$total['tot3'] = ($numwelds + $numconwelds) + $fortot2/$fortot3;
$response = json_encode($total);
header("Content-Type: application/json");
echo $response;
exit;
echo '<pre>';
var_dump($response);
echo '</pre>';
刪除'退出;'或全部打印語句 – ponciste
@ponciste謝謝,我已經試過了之後移動它,但它似乎沒有任何工作。 – mattnewbie
對不起,你必須做'echo json_encode($ total,JSON_PRETTY_PRINT);' – ponciste