0

I have a products table that has foreign characters. Now I am trying to use the php array in javascript to narrow a dropdown box as the user types. Everything works except when there is a foreign character (like a carrot or tilde above a letter). How can I encode the the php array into the javascript array with these characters?

$jpro=json_encode($pro, JSON_HEX_APOS|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE);
var phpPro=<?php echo $jpro; ?>;

The errors Im getting are

var phpPro=;
"Uncaught SyntaxError: Unexpected token ;"
Reed
  • 14,703
  • 8
  • 66
  • 110
  • 2
    Have you tried, simply: `json_encode($pro, JSON_UNESCAPED_UNICODE);` [src](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through/279279#279279)? – Reed Jul 16 '19 at 20:15
  • Is `json_encode` outputting anything? It looks like it's not. And what is the input php array? That might be relevant. – Reed Jul 16 '19 at 20:18
  • 1
    one of the flags is causing json_encode to return null. Start with no flags at all and see if you get output then. If so, add one flag at a time until you can identify which one is causing the issue. Then you can go back to your array and try to clean that out ahead of time. – dwenr Jul 16 '19 at 20:32
  • I just tried the json_encode($pro, JSON_UNESCAPED_UNICODE); --- did not work. This program is acutally used by numorous companies and the php array works along with the js until a foreign char is encountered. When finding the record that causes the issue it was..."Amigó" where the error occurred. There are Spanish and French characters in the data. So I know this is the issue. – wolfpride71 Jul 16 '19 at 20:57

1 Answers1

-1

With JSON_HEX_APOS|JSON_HEX_QUOT you're telling json_encode to convert apostrophes and quotes to \u0027 and \u0022 respectively, but then negate it with JSON_UNESCAPED_UNICODE. And the reason you're getting that error is because you haven't put the PHP code in the variable inside quotes.

Now, since json_encode returns an array with quotes, e.g. [ "Österreich", "Gemüse" ], you can't just place the code inside them, but need to use apostrophes.

Ultimately, the following would be correct*:

$jpro = json_encode($pro, JSON_UNESCAPED_UNICODE);
var phpPro = '<?php echo $jpro; ?>';

Granted, this is going to return a string, not an array if that was your intention. If you want to transform it back to an array, just use JSON.parse:

var phpPro = JSON.parse('<?php echo $jpro; ?>');

* correct in the sense that it does what you're asking, but you really should never mix JavaScript and PHP together — especially not if you're dealing with user input.

neoto
  • 359
  • 1
  • 7
  • I added the 3 lines and it returned this... Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse () – wolfpride71 Jul 16 '19 at 21:19
  • Can you post at least a part of what you **$pro** array looks like? – neoto Jul 16 '19 at 21:21
  • What would be the best way to import my product ids and names from mysql into the js? JS is my kryptonite! – wolfpride71 Jul 16 '19 at 21:23
  • var phpPro = '[{"idpro":"993","0":"993","producer":"ADC","1":"ADC","name":"EVOO","2":"EVOO","vintage":"","3":""},{"idpro":"1056","0":"1056","producer":"ADC","1":"ADC","name":"Kaid Sauvignon ","2":"Kaid Sauvignon ","vintage":"2015","3":"2015"}, – wolfpride71 Jul 16 '19 at 21:25
  • That is a completely different question altogether and depends on your table structure. Anyway, if I append a closing **]'** and put that whole thing inside `JSON.parse`, it parses your array with no issues. You also didn't give me the original PHP array, but supposedly the resulting string of it. – neoto Jul 16 '19 at 21:33
  • I put the brackets inside the ' and it is working. Thank you! How do I give you a star or a thumbs up? – wolfpride71 Jul 16 '19 at 21:38
  • var phpPro = JSON.parse('[{"idpro":"993","0":"993","producer":"ADC","1":"ADC","name":"EVOO","2":"EVOO","vintage":"","3":""},{"idpro":"1056","0":"1056","producer":"ADC","1":"ADC","name":"Kaid Sauvignon ","2":"Kaid Sauvignon ","vintage":"2015","3":"2015"} **]'**); You pasted it wrongly or something, I don't know. That is why I am asking you to update your question with the **$pro** PHP variable so we know what's going on. – neoto Jul 16 '19 at 21:42
  • Those `json_encode` flags can coexist quite happily; they have nothing to do with each other and certainly don't cause errors when used together. And why would you suggest putting the JSON into quotes and then parsing it? JSON is a subset of JavaScript, and can be put directly into a document. – miken32 Jul 17 '19 at 19:00
  • Because I tried it myself and that's how it behaved, unless I'm blind, stupid, or both. And that is not what I am suggesting at all. He is supposedly getting that array from the `` part, which does work even if you don't put quotes, but in my case, VSCode complained without them. – neoto Jul 17 '19 at 20:03