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