简体   繁体   English

回声句子中出现引号

[英]Quote marks appearing in echo sentence

I'm building a webpage using PHP. 我正在使用PHP构建网页。 I have a table with multiple buttons. 我有一个带有多个按钮的桌子。 I'm building the buttons in a for loop using the following code: 我使用以下代码在for循环中构建按钮:

        echo "<tr><td><input type='button' value='".$row['Descripcion']."'";
        echo "onclick=EliminarHashTag('".$row['ID']."','";
        echo $row['Descripcion'];
        echo "','Norma','".$Codigo."','".$Organizacion."')> </td></tr>";   

It all works fine as long as my variables are strings made of single words.. For example, if it's $row['Descripcion']="manometros" , (string without whitespace) the echo outputs the following: 只要我的变量是由单个单词组成的字符串,这一切都可以正常工作。例如,如果它是$row['Descripcion']="manometros" ,(不带空格的字符串),则回显将输出以下内容:

<input type="button" value="Manometros" onclick="EliminarHashTag('14','Manometros','norma','k-300','pdvsa')="">

but if my variable is made of a string with whitespace $row['Descripcion']="Criterios Generales" , the echo outputs quote marks in the whitespace, messing up the javascript function call syntax and hence making the code not work (Criterios" Generales). 但是,如果我的变量是由带有空格$row['Descripcion']="Criterios Generales"的字符串组成的,则echo将在空格中输出引号,从而弄乱了javascript函数调用语法,从而使代码无法正常工作(Criterios”将军)。

<input type="button" value="Criterios Generales" onclick="EliminarHashTag('14','Criterios" generales','norma','k-300','pdvsa')="">

I read the echo documentation at php.net, but I saw no mention of this issue. 我在php.net上阅读了echo文档,但是我没有提到这个问题。 Am I doing something wrong? 难道我做错了什么?

If you do not quote an attribute, the attributes value ends at the first whitespace or the end of the tag. 如果您不引用属性,则属性值在第一个空格或标记的结尾处结束。

Add quotes around the onclick... 在onclick周围添加引号...

echo " onclick=\"EliminarHashTag('".$row['ID']."','";
      ^^       ^^       

and

echo "','Norma','".$Codigo."','".$Organizacion."')\"> </td></tr>"; 
                                                  ^^

Now if any of the values have double quotes they need to be escaped as html entitles. 现在,如果任何值都带有双引号,则需要将其转义为html标题。 Or you would need to swap the single for double. 否则,您需要将单身换成双身。

Your HTML is invalid, debugging tools like firebug and chrome developer tools try to correct it to display a proper domtree. 您的HTML无效,调试工具(例如firebug和chrome开发人员工具)会尝试对其进行更正以显示正确的domtree。

You're not only missing the closing quote on your onclick attribute but also break the encapsulation by using the same type of quotes inside your onclick function arguments: 您不仅会丢失onclick属性的右引号,而且还会通过在onclick函数参数中使用相同类型的引号来破坏封装:

 echo '<tr><td><input type="button" value="'.$row['Descripcion'].'"';
 echo 'onclick="EliminarHashTag(\''.$row['ID'].'\',\'';
 echo $row['Descripcion'];
 echo '\',\'Norma\',\''.$Codigo.'\',\''.$Organizacion.'\')"> </td></tr>';   

Try escaping the quotes with back slashes: 尝试用反斜杠转义引号:

    echo "onclick=EliminarHashTag('".$row['ID']."',\"";
    echo $row['Descripcion'];
    echo "\",'Norma','".$Codigo."','".$Organizacion."')> </td></tr>";

I changed the ' before and after your variable to \\" . 我将您的变量前后的'更改为\\"

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

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