json_decode gives no data on print_r, but raw data present in $_GET

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



json_decode gives no data on print_r, but raw data present in $_GET



I have passed an array in AJAX using


htmlspecialchars(json_encode($cInfo))



In my other php file, if I do


print_r ($_GET);



I can see all the data that was passed via AJAX, like this:


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" )



And yet, if have the following code:


$cInfo = json_decode($_GET['cInfo']);



and then try to print_r($cInfo) I get no data at all. I don't understand why I can't use the data when it's obviously present in $_GET



More detailed code added below.



The php generating the form that submits the json encoded array and other data:


$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>';



The jQuery that submits the form:


$(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;
);
);



The start of the file that AJAX submits to, where $_GET is showing the correct data that I can't get into a usable array


<?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);



The output from print_r($_GET);


/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"...





If $_GET is already an array, I don't think you need to decode anything unless it's in string form. Does $cInfo = $_GET['cInfo']; work?
– ggorlen
Aug 12 at 18:58



$_GET


$cInfo = $_GET['cInfo'];





@ggorlen No. If i set that, and then print_r($cInfo) I get nothing.
– Steph3071
Aug 12 at 21:08





Bummer, worth a try. Can you post a toy example of your two PHP scripts?
– ggorlen
Aug 12 at 21:14





@ggorlen I've added a bunch of original code to the original question
– Steph3071
Aug 12 at 22:07





All right, that's a bit helpful, but I'm looking for a minimal and verifiable example. I don't have $categories and I can't tell what your HTML looks like, so that's not helpful. The important part is the $_GET dump, but that ends with ..., so I can't see the rest of the string you're trying to decode. Please post the result of echo $_GET['cInfo'];. It should be a string of length 769, and if it's not a valid JSON encoded structure, that's likely why it's not decoding properly and is where json_last_error (in the answer posted) might be helpful.
– ggorlen
Aug 12 at 22:48


$categories


$_GET


...


echo $_GET['cInfo'];


json_last_error




1 Answer
1



If there are a problem decoding the json, the function could return null.



If you want to know the problem, you can use the functions json_last_error or json_last_error_msg.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard