0

I am trying to insert some Options to select which are taken from DB and are being placed also to a jQuery code. I used json_encode, but that did not fix all the problems.

Basically, the / are escaped and there are only single quotes, which is good, but it places double quote at the start and at the end of the string:

Sample code that I see in console:

"<div style='display: inline-block; width: 30%;'>"+
    "Article categories:"+
    "<br>"+
    "<select name='article_cat' class='textbox' style='width:250px;'>"<option value='1'>Random<\/option>\n"</select>"+
"</div>"+

With this error code in console:

Uncaught SyntaxError: Unexpected identifier

This is the jQuery code in PHP file itself:

var options4 = {
    dataType: 'json',
    success:    function(output) {
    $("... BLA BLA BLA .... 40 lines of code....."+
    "<div style='display: inline-block; width: 30%;'>"+
        "Article categories:"+
        "<br>"+
        "<select name='article_cat' class='textbox' style='width:250px;'><?php echo json_encode($art_catlist); ?></select>"+
    "</div>"+
    "Bla bla bla .... another 20 lines of code"+
    "</div>").insertBefore("#new_background_header");
    Some more jQuery code......
}

Does somebody have idea why that happens?

This code creates the list of options (it has to be usable in PHP too, so cant really change it a lot):

while ($data = dbarray($result)) {
    $art_catlist .= "<option value='".$data['article_cat_id']."'>".$data['article_cat_name']."</option>\n";
}

Screen from console: enter image description here

Ps.: Why you remove Hi at the start of the message? Wanted to say Hi and thanks for every help. Now it seems that I am being rude :(

MiChAeLoKGB
  • 796
  • 14
  • 38
  • That code in PHP is the jQuery code, but in PHP file. THe top one is result I see in Developers console in Chrome. Sorry for misundertanding. Edit: Do not delete your comments, c'mon .... – MiChAeLoKGB Jan 28 '15 at 18:26
  • jQuery isn't to be found anywhere in your sample code. It's all just HTML and why are you JSON encoding the options? Delete that and you should be fine. Also the `\n` from the while loop. – Mouser Jan 28 '15 at 18:27
  • 1
    `Why you remove Hi at the start of the message?` — because it's actually useless and takes another second to read. A good-formulated question is already a sign of politeness to the community ;) – Alex Shesterov Jan 28 '15 at 18:28
  • 1
    http://stackoverflow.com/questions/5673269/what-is-the-advantage-of-using-heredoc-in-php – fortune Jan 28 '15 at 18:29
  • @AlexShesterov Well, just wanted to be nice :) Whole code is jQuery, but the code itself has about 75 lines, so I did not place it whole here. – MiChAeLoKGB Jan 28 '15 at 18:29
  • I never really understood the desire to print all HTML code on the server. Just send/print the data as JSON and let the browser render the option list on the client. Saves server usage. Client pc's nowadays are powerful enough to do comprehensive tasks, so iterating over a few values shouldn't be a problem. – Mouser Jan 28 '15 at 18:32
  • @Mouser The thing is, it has to be printed by server. The jQuery is only if you add new option, so it prints the whole code without refreshing the page. I changed the code in question, so you can see its jQuery. – MiChAeLoKGB Jan 28 '15 at 18:34
  • @MiChAeLoKGB, I disagree. The fetching of the data has to be done on the server. You can print the result as JSON an iterate over the returned object adding one option per iteration to the select. – Mouser Jan 28 '15 at 18:36
  • Post the whole code. `Syntax Error` indicates something's wrong there. – Alejandro Iván Jan 28 '15 at 18:37
  • @Mouser Not when you have to display all results from DB as a list. The jQuery is there to only eamlesly add new item you add to the list. Please, I just want to fix this one problem as othervise it working well. Can't now completly change whole CMS and way how that admin works, cos you think returning data from DB is better to be done as JSON. Sorry. – MiChAeLoKGB Jan 28 '15 at 18:39
  • Btw I'm just guessing, but shouldn't this `""` be this? `""` – Alejandro Iván Jan 28 '15 at 18:39
  • @AlejandroIván The problem are those " right in front of – MiChAeLoKGB Jan 28 '15 at 18:39
  • @MiChAeLoKGB yep. As `json_encode()` is a PHP function that converts arrays, strings, structures, etc. to JSON-compatible code, the conversion of a `string` is, well... the Javascript representation of a string (I mean, including the `"` double quotes). Just print without the `json_encode()` as it's already HTML. If you need to **escape** characters, `htmlentities()` and similar functions should work fine. – Alejandro Iván Jan 28 '15 at 18:46

1 Answers1

1
  • $art_catlist — is a string in PHP. Its contents is: <option value='1'>Random<\/option>.

  • json_encode($art_catlist) — produces JSON-representation of this string, i.e. the result contains double-quotes. The contents of the string is: "<option value='1'>Random<\/option>"

  • "<select name='article_cat' class='textbox' style='width:250px;'><?php echo json_encode($art_catlist); ?></select>" — produces the following output:

    "<select name='article_cat' class='textbox' style='width:250px;'>"<option value='1'>Random<\/option>"</select>".

    "Simplified", JavaScript sees the following tokens: "some string"<option value ...

    So in JS you have a string literal, then comparison operator, then an identifier, then (and this is the syntax error in JS) space and another identifier.


The simplest solution is to add string concatenation, which is run by JS.
PHP code:

"<select name='article_cat' class='textbox' style='width:250px;'>" 
+ <?php echo json_encode($art_catlist); ?> 
+ "</select>"

Another way is to remove quotes from JSON-encoded string on PHP side.

Community
  • 1
  • 1
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103