wavern
wavern

Reputation: 69

PHP json encode error in Javascript

I'm having the following error on an HTML page.

Uncaught SyntaxError: page URL Here Unexpected token ;

This is the code (variables are defined elsewhere and do exist.

<script>
    statesDropDown = <?php echo json_encode($this->states);?>;
    programIdsDropDown = <?php echo json_encode($this->programIdsData);?>;
</script>

This is the results of the above on the page

<script>
    statesDropDown = [{"id":"1","code":"AL","name":"Alabama"},{"id":"2","code":"AK","name":"Alaska"},{"id":"3","code":"AZ","name":"Arizona"},{"id":"4","code":"AR","name":"Arkansas"},{"id":"5","code":"CA","name":"California"},{"id":"6","code":"CO","name":"Colorado"},{"id":"7","code":"CT","name":"Connecticut"},{"id":"8","code":"DE","name":"Delaware"},{"id":"9","code":"FL","name":"Florida"},{"id":"10","code":"GA","name":"Georgia"},{"id":"11","code":"HI","name":"Hawaii"},{"id":"12","code":"ID","name":"Idaho"},{"id":"13","code":"IL","name":"Illinois"},{"id":"14","code":"IN","name":"Indiana"},{"id":"15","code":"IA","name":"Iowa"},{"id":"16","code":"KS","name":"Kansas"},{"id":"17","code":"KY","name":"Kentucky"},{"id":"18","code":"LA","name":"Louisiana"},{"id":"19","code":"ME","name":"Maine"},{"id":"20","code":"MD","name":"Maryland"},{"id":"21","code":"MA","name":"Massachusetts"},{"id":"22","code":"MI","name":"Michigan"},{"id":"23","code":"MN","name":"Minnesota"},{"id":"24","code":"MS","name":"Mississippi"},{"id":"25","code":"MO","name":"Missouri"},{"id":"26","code":"MT","name":"Montana"},{"id":"27","code":"NE","name":"Nebraska"},{"id":"28","code":"NV","name":"Nevada"},{"id":"29","code":"NH","name":"New Hampshire"},{"id":"30","code":"NJ","name":"New Jersey"},{"id":"31","code":"NM","name":"New Mexico"},{"id":"32","code":"NY","name":"New York"},{"id":"33","code":"NC","name":"North Carolina"},{"id":"34","code":"ND","name":"North Dakota"},{"id":"35","code":"OH","name":"Ohio"},{"id":"36","code":"OK","name":"Oklahoma"},{"id":"37","code":"OR","name":"Oregon"},{"id":"38","code":"PA","name":"Pennsylvania"},{"id":"39","code":"RI","name":"Rhode Island"},{"id":"40","code":"SC","name":"South Carolina"},{"id":"41","code":"SD","name":"South Dakota"},{"id":"42","code":"TN","name":"Tennessee"},{"id":"43","code":"TX","name":"Texas"},{"id":"44","code":"UT","name":"Utah"},{"id":"45","code":"VT","name":"Vermont"},{"id":"46","code":"VA","name":"Virginia"},{"id":"47","code":"WA","name":"Washington"},{"id":"48","code":"WV","name":"West Virginia"},{"id":"49","code":"WI","name":"Wisconsin"},{"id":"50","code":"WY","name":"Wyoming"}];
    programIdsDropDown = ;
</script>

I've removed the encode and confirm that I get a cannot convert array to string error. Why is this returning blank here and causing the before mentioned error?

This is code that was moved from a 2012 Windows Server to a 2008 Windows Server and ran completely fine in 2012. Are there any known issues with this JS call in 2008? This is used within AngularJS but at this point of error is independent of the AngularJS so is just basic JS/PHP.

Many thanks.

Upvotes: 1

Views: 1993

Answers (2)

vishva8kumara
vishva8kumara

Reputation: 355

What you can do is to first avoid echo the json_encode return value. First assign it to a php variable and check if it is an empty string, then echo '[]'

Upvotes: 0

Guillaume Sainthillier
Guillaume Sainthillier

Reputation: 1685

If json_encode returns an empty string, it's maybe due to the fact that the datas contain non ASCII caracters. Try to encode them in utf-8 like this :

<script>
    statesDropDown = <?php echo json_encode($this->states);?>;
    programIdsDropDown = <?php echo json_encode(array_map('utf8_encode', $this->programIdsData));?>;
</script>

Upvotes: 2

Related Questions