简体   繁体   English

从 PHP 调用 javascript 函数 - 引号问题

[英]calling javascript function from PHP - quotes issue

I'm trying to call javascript function from PHP.我正在尝试从 PHP 调用 javascript 函数。 the idea is to create each parameter from file as link that when clicked inputs it's value into text box.这个想法是创建的每个参数从文件链接,点击输入时,它的价值为文本框。

$lines = file('http://localhost/repositories.txt');
foreach ($lines as $line_num => $line) {
    echo  "<a onclick='javascript:(document.folderForm.folderLocation.value=".htmlspecialchars($line) .");' href='#'>".htmlspecialchars($line) . "</a><br />";
}

when clicking the created links I'm getting (for example):单击我得到的创建的链接时(例如):

ReferenceError: test2 is not defined

I think there should be quotation marks around the parameter, but I can't add them.我认为参数周围应该有引号,但我无法添加它们。 I tried to use ' \\" ' without success. any ideas?我尝试使用 ' \\" ' 没有成功。有什么想法吗?

You need to quote and properly escape the JavaScript string that you're assigning to the input value property.您需要引用并正确转义分配给输入value属性的 JavaScript 字符串。 You can use json_encode for that:您可以为此使用json_encode

echo "<a onclick='document.folderForm.folderLocation.value=".htmlspecialchars(json_encode($line)) .";' href='#'>".htmlspecialchars($line) . "</a><br />";

Demo演示


The main cause of the issue is that your code was missing the quotes around the string value, so JavaScript was interpreting it as an identifier (variable name).问题的主要原因是您的代码缺少字符串值周围的引号,因此 JavaScript 将其解释为标识符(变量名称)。

json_encode adds quotes around the value and, more importantly, escapes the value to make sure it is a valid JSON string (it escapes line feeds, carriage returns and quotes which could easily result in a syntax error otherwise). json_encode在值周围添加引号,更重要的是,对值进行转义以确保它是有效的 JSON 字符串(它转义换行、回车和引号,否则很容易导致语法错误)。

JSON is a subset of the JavaScript syntax, so any valid JSON is valid JavaScript. JSON 是 JavaScript 语法的一个子集,因此任何有效的 JSON 都是有效的 JavaScript。 (provided it is used in a suitable context) (前提是在合适的上下文中使用)

Finally, as the value is inside an HTML attribute, htmlspecialchars is still applied to escape the value.最后,由于该值位于 HTML 属性内,因此仍会应用htmlspecialchars来转义该值。 It escapes quotes to make sure the variable value does not break out of the attribute value and also escapes characters such as & to not be interpreted as the beginning of an HTML entity sequence.它会转义引号以确保变量值不会超出属性值,并且还会转义诸如&字符,以免被解释为 HTML 实体序列的开头。


Finally, I would recommend against echo in this case, as to reduce the number of quotes and make interpolation easier to read:最后,在这种情况下,我建议不要使用echo ,以减少引号的数量并使插值更易于阅读:

$lines = file('http://localhost/repositories.txt');
foreach ($lines as $line_num => $line) { ?>
    <a onclick='document.folderForm.folderLocation.value=<?= htmlspecialchars(json_encode($line)) ?>;' href='#'><?= htmlspecialchars($line) ?></a><br />
<?php } ?>

Demo演示

Note: PHP short echo tags are available everywhere independent of configuration as of PHP 5.4.注意:自 PHP 5.4 起,PHP 短回显标签随处可用,与配置无关。 Switch <?= to <?php echo if you need to support PHP < 5.4.如果您需要支持 PHP < 5.4,请将<?=切换到<?php echo

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

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