dongyue0225 2014-12-31 03:38
浏览 60
已采纳

引号出现在echo句中

I'm building a webpage using PHP. I have a table with multiple buttons. I'm building the buttons in a for loop using the following code:

        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:

<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).

<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. Am I doing something wrong?

  • 写回答

3条回答 默认 最新

  • doujian3132 2014-12-31 03:52
    关注

    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...

    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. Or you would need to swap the single for double.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助