简体   繁体   English

PHP字符串与Javascript兼容(引号)

[英]PHP string to Javascript compatible (quotes)

I am trying to insert some Options to select which are taken from DB and are being placed also to a jQuery code. 我试图插入一些选项以选择从数据库中获取的选项,并将它们也放置到jQuery代码中。 I used json_encode, but that did not fix all the problems. 我使用了json_encode,但是并不能解决所有问题。

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: 这是PHP文件本身中的jQuery代码:

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): 这段代码创建了选项列表(它也必须在PHP中可用,因此不能做很多更改):

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

Screen from console: 控制台屏幕: 在此处输入图片说明

Ps.: Why you remove Hi at the start of the message? 附注:为什么您要在讯息开头删除Hi? Wanted to say Hi and thanks for every help. 想打个招呼,谢谢大家的帮助。 Now it seems that I am being rude :( 现在看来我很粗鲁:(

  • $art_catlist — is a string in PHP. $art_catlist —是PHP中的字符串。 Its contents is: <option value='1'>Random<\\/option> . 它的内容是: <option value='1'>Random<\\/option>

  • json_encode($art_catlist) — produces JSON-representation of this string, ie the result contains double-quotes. json_encode($art_catlist) —生成此字符串的JSON表示,即结果包含双引号。 The contents of the string is: "<option value='1'>Random<\\/option>" 字符串的内容是: "<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;'><?php echo json_encode($art_catlist); ?></select>" —产生以下输出:

    "<select name='article_cat' class='textbox' style='width:250px;'>"<option value='1'>Random<\\/option>"</select>" . "<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 ... “简化”,JavaScript看到以下标记: "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. 因此,在JS中,您有一个字符串文字,一个比较运算符,一个标识符,然后(这是JS中的语法错误)空间和另一个标识符。


The simplest solution is to add string concatenation, which is run by JS. 最简单的解决方案是添加由JS运行的字符串连接。
PHP code: PHP代码:

"<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 . 另一种方法是在PHP端从JSON编码的字符串中删除引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM