单引号内的单引号PHP

时间:2013-05-18 08:02:36

标签: php javascript variables quotes html-escape-characters

我有一个HTML achor标签,如下所示:

echo '<a href="javascript:tempBuy('.$res_get_price[0][0].','.$res_get_price[0][1].','.$res_get_price[0][2].','.$dt_str.')">'.$res_get_price[0][0];

相应的javascript函数tempBuy()是

function tempBuy(rate,veg_name,market_name,dt)
{
      alert(dt);
}

但问题是它根本不警觉!可能我需要在tempBuy()函数中的单引号中包含变量名。我试过tempBuy(\'var1'\,\'var2\'...),但它显示错误。我怎么能够这样做。谢谢 。

该部分的来源显示如下:

<td width="120px" class=""><a href="javascript:tempBuy(56.0,Apple,Bangalore,2013-05-18)">56.0</a>                                
                                 </td>
                                <script>
                                    function tempBuy(rate,veg_name,market_name,dt)
                                    {
                                        alert(rate);

                                    }
                                </script>

4 个答案:

答案 0 :(得分:1)

您没有将javascript参数包装在引号中。您需要将每个变量用单引号括起来,因为您对“href”属性使用了双引号。另一件事是你没有关闭“一个”HTML标签。

echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\',\''.$res_get_price[0][1].'\',\''.$res_get_price[0][2].'\',\''.$dt_str.'\')">'.$res_get_price[0][0].'</a>';

答案 1 :(得分:1)

如果变量中的任何内容不是有效的javascript文字,则必须使其成为如下字符串:

echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\' ...

如果你的变量中有'你也必须用\'替换它们。

答案 2 :(得分:0)

从渲染输出中可以看出,您需要引用最后3个非数字参数。正确的输出应为:javascript:tempBuy(56.0,'Apple','Bangalore','2013-05-18')

正确的PHP代码是:

echo '<a href="javascript:tempBuy('.$res_get_price[0][0].',\''.$res_get_price[0]`[1].'\',\''.$res_get_price[0][2].'\',\''.$dt_str.'\')">'.$res_get_price[0][0].'</a>';`

答案 3 :(得分:0)

echo "<a href=\"javascript:tempBuy('".$res_get_price[0][0]."','".$res_get_price[0][1]."','".$res_get_price[0][2]."','".$dt_str."')\">".$res_get_price[0][0];