I am trying to pass my php array to jQuery array but I am having this error on chrome console Uncaught SyntaxError: Unexpected token <
functions.php
/*Call Jquery for dropdowns*/
function mytheme_custom_scripts(){
if ( is_page('Support')) {
$scriptSrc = get_stylesheet_directory_uri() . '/js/freshdeskdata.js';
wp_enqueue_script( 'myhandle', $scriptSrc , array(), '1.0', false );
}
}
add_action( 'wp_enqueue_scripts', 'mytheme_custom_scripts' );
/*End - Jquery*/
add_filter('wpcf7_form_tag_data_option', function($n, $options, $args){
...
// Category
if (in_array('category.provider', $options)){
...
// Getting results
$result = curl_exec($ch); // Getting jSON result string
curl_close($ch);
$data = json_decode($result, true);
global $choice; // This is my array
$choice = $data[0]['choices'];
ksort($choice);
return array_keys($choice);
}
// default - do not apply any changes within the options
return null;
}, 10, 3);
/js/freshdeskdata.js
jQuery(function ($) {
$(document).ready(function() {
$("#main-category").on('change', function() {
var arrayFromPHP = <?php echo json_encode($choice); ?>;
var $el = $("#sub-category");
$el.empty();
alert("arrayFromPHP");
});
});
});
<?php echo json_encode($choice); ?>
bit in quote marks, but even that, I suspect, won't work the way you want it to. WordPress allows you to pass data from PHP to Javascript, but you need to usewp_localize_script()
to do it properly.wp_localize_script
as said Pat J.