Php json_decode在print_r上不提供数据,但在$u GET中提供原始数据
我已经使用AJAX传递了一个数组Php json_decode在print_r上不提供数据,但在$u GET中提供原始数据,php,json,Php,Json,我已经使用AJAX传递了一个数组 htmlspecialchars(json_encode($cInfo)) 在我的另一个php文件中,如果我这样做 print_r ($_GET); 我可以看到通过AJAX传递的所有数据,如下所示: Array ( [action] => edit_category [cPath] => [cID] => 141 [cInfo] => {"categories_id":"141","categories_name":"Mens","c
htmlspecialchars(json_encode($cInfo))
在我的另一个php文件中,如果我这样做
print_r ($_GET);
我可以看到通过AJAX传递的所有数据,如下所示:
Array ( [action] => edit_category [cPath] => [cID] => 141 [cInfo] => {"categories_id":"141","categories_name":"Mens","categories_description":"Get ready for warmer days with our new season trends for Spring\/Summer. Whether you're after an iconic Superdry jacket, a pair of great fitting jeans or a cool tee, shop our latest collections right here. Discover premium quality outerwear for those changeable spring days and style with the latest T-shirts, shirts and trainers.","categories_image":"categories\/cat-head-1-min.jpg","parent_id":"0","sort_order":"10","date_added":"2016-09-15 23:43:02","last_modified":"2018-02-21 15:52:14","categories_status":"1"} )
但是,如果您有以下代码:
$cInfo = json_decode($_GET['cInfo']);
然后试着打印我一点数据都没有。我不明白为什么我不能使用数据,因为它显然存在于$\u GET中
下面添加了更详细的代码
php生成提交json编码数组和其他数据的表单:
$lc_text .= '<form id="category-edit-'.$categories->fields['categories_id'].'" class="tooltip-category-edit-'.$categories->fields['categories_id'].'" data-tooltip-content="#category-edit-tooltip-content-'.$categories->fields['categories_id'].'">
<input type="hidden" name="action" value="edit_category" />
<input type="hidden" name="cPath" value="'.$cPath.'" />
<input type="hidden" name="cID" value="'.$categories->fields['categories_id'].'" />
<input type="hidden" name="cInfo" value="'.htmlspecialchars(json_encode($cInfo)).'" />
<input type="image" class="icon-edit" src="images/icon_edit.png" border="0" alt="'.ICON_EDIT.'" title="'.ICON_EDIT.'" />
</form>
<div class="tooltip_templates">
<div id="category-edit-tooltip-content-'.$categories->fields['categories_id'].'">
<div class="tooltip-loading-'.$categories->fields['categories_id'].' load-text">Loading...</div>
<div class="edit-category-results-'.$categories->fields['categories_id'].'"></div>
</div>
</div>';
如果解码json时出现问题,函数可能返回null
如果你想知道这个问题,你可以使用函数json\u last\u error或json\u last\u error\u msg如果
$\u GET
已经是一个数组,我认为你不需要解码任何东西,除非它是字符串形式。$cInfo=$\u是否获得['cInfo']代码>工作?@ggorlen不。如果我设置了,然后打印($cInfo),我什么也得不到。糟糕,值得一试。你能发布两个PHP脚本的玩具示例吗?@ggorlen我在原始问题中添加了一堆原始代码好吧,这有点帮助,但我正在寻找一个示例。我没有$categories
,我也不知道你的HTML是什么样子,所以这没有帮助。重要的部分是$\u GET
转储,但它以..
结尾,因此我看不到您试图解码的字符串的其余部分。请发布echo$_GET['cInfo']的结果代码>。它应该是一个长度为769的字符串,如果它不是一个有效的JSON编码结构,这可能就是它没有正确解码的原因,JSON\u last\u error
(在发布的答案中)可能会有所帮助。
$(document).ready(function() {
// Variable to hold request
var request;
$('.tooltip-category-edit-<?php echo $categories->fields['categories_id']; ?>').tooltipster({
plugins: ['sideTip', 'scrollableTip'],
trigger: 'click',
interactive: 'true',
maxWidth: 600,
contentAsHTML: 'true',
functionAfter: function() {
// Add background overlay
if ($('#overlay-mask').length) {
$('#overlay-mask').hide();
}
},
functionBefore: function(instance, helper) {
var $origin = $(helper.origin);
// Add background overlay
if (!$('#overlay-mask').length) {
$('body').append('<div id="overlay-mask" style="display: block;"></div>');
} else {
$('#overlay-mask').show();
}
if ($origin.data('loaded') !== true) {
// Abort any pending request
if (request) {
request.abort();
}
// setup local variables
var $form = $('.tooltip-category-edit-<?php echo $categories->fields['categories_id']; ?>');
// Let's select and cache the fields
var $inputs = $form.find();
// Serialize the data in the form
var serializedData = $form.serialize();
// Send the request
request = $.ajax({
url: "categories_edit_ajax.php",
type: "get",
data: serializedData
});
// Callback handler on success
request.done(function (response, textStatus, jqXHR){
instance.content(response);
$origin.data('loaded', true);
});
}
}
});
// Variable to hold request
var request;
// Bind to the submit event of our form
$("#category-edit-<?php echo $categories->fields['categories_id']; ?>").submit(function(event){
// Prevent default posting of form
event.preventDefault();
return false;
});
});
<?php
$heading = array();
$contents = array();
$cPath = isset($_GET['cPath']) ? filter_var(trim($_GET['cPath']), FILTER_SANITIZE_STRING) : null;
$cID = isset($_GET['cID']) ? filter_var(trim($_GET['cID']), FILTER_SANITIZE_STRING) : null;
$cInfo = json_decode($_GET['cInfo']);
echo '<div class="debug">';
echo "get content: ";
print_r($_GET);
echo '<br/>';
echo "cPath: ".$cPath.'<br/>';
echo "cID: ".$cID.'<br/>';
echo "cInfo: "; print_r($cInfo).'<br/>';
echo '</div>';
$on_image_delete = false;
$off_image_delete = true;
$heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_EDIT_CATEGORY . '</b>');
$contents[] = array('text' => zen_draw_form('categories', FILENAME_CATEGORIES, 'action=update_category&cPath=' . $cPath . ((isset($_GET['search']) && !empty($_GET['search'])) ? '&search=' . $_GET['search'] : ''), 'post', 'enctype="multipart/form-data"') . zen_draw_hidden_field('categories_id', $cInfo->categories_id));
$contents[] = array('text' => TEXT_EDIT_INTRO);
/Applications/MAMP/htdocs/justsimplecart/jscadmin/categories_edit_ajax.php:22: array(4) { 'action' => string(13) "edit_category" 'cPath' => string(0) "" 'cID' => string(3) "141" 'cInfo' => string(769) "{"categories_id":"141","categories_name":"Mens","categories_description":"Get ready for warmer days with our new season trends for Spring\/Summer. Whether you're after an iconic Superdry jacket, a pair of great fitting jeans or a cool tee, shop our latest collections right here. Discover premium quality outerwear for those changeable spring days and style with the latest T-shirts, shirts and trainers.","categories_image":"categories\"... }