简体   繁体   English

PHP代码中的额外神秘空间

[英]extra mystery space in php code

I am trying to pass a variable frm php to a javascript function, however a space keeps getting appeneded, and I can not see how. 我正在尝试将变量frm php传递给javascript函数,但是空格不断出现,我看不到如何。

The relevant php code snippet: 相关的php代码段:

<p><a href='#' onclick=\"makewindows(" . $html . "); return false;\">Click for full description </a></p>".$brand."
        <p><a href=\"#\" onclick=\"deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>

$brand is what I want to pass, and deleteRec is the name of the function. $ brand是我想要传递的,deleteRec是函数的名称。

At the end of the first line I am echoing out brand before the link to deleteRec, and it contains no space. 在第一行的末尾,我要在指向deleteRec的链接之前回显brand,它不包含任何空格。 In my test case, it is set to simply 'o'. 在我的测试用例中,它设置为简单的“ o”。

The link that is genereated for deleteRec however, clearly contains a space, and I don't know where it is coming from. 但是,为deleteRec生成的链接显然包含一个空格,并且我不知道它从哪里来。

<a href="#" onclick="deleteRec('190274380300', ' o', '2', '250343889611')">DELETE</a>

Do var_dump($brand) and look closely - there's almost certainly a space in it! 做var_dump($ brand)并仔细查看-几乎可以肯定有一个空格!

In which case, you can guard against it with trim 在这种情况下,可以修剪一下

$brand=trim($brand);

Change: 更改:

<p><a href=\"#\" onclick=\"deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>

to: 至:

<p><a href=\"#\" onclick=\"deleteRec('".$ARTICLE_NO."', '".trim($brand)."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>

and tell us how it goes. 并告诉我们进展如何。

Try do echo the following: 尝试执行以下操作:

echo "--$brand--";

This way you'll be able to see if there are any spaces in the variable. 这样,您将可以查看变量中是否有空格。

As a general matter of style, I would change second link from: 作为样式的一般问题,我将第二个链接从:

<a href=\"#\" onclick=\"
      deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>

to: 至:

<?php
$deleteRecArgs = "'$ARTICLE_NO', '$brand', '$pg', '$nextArticleNo'";
?>
<a href="#" onclick="deleteRec(<?php echo $deleteRecArgs?>)">DELETE</a>

It's easier to read and maintain. 它更易于阅读和维护。

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

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