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 :(

13
  • 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 .... Commented Jan 28, 2015 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. Commented Jan 28, 2015 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 ;) Commented Jan 28, 2015 at 18:28
  • 1
    stackoverflow.com/questions/5673269/… Commented Jan 28, 2015 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. Commented Jan 28, 2015 at 18:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I completly forgot to try to add just "+ there. Tought it would break the jQuery itself. But it works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.