将带有撇号的字符串从PHP传递给JavaScript函数 [英] Passing string with apostrophe from PHP into JavaScript function

查看:102
本文介绍了将带有撇号的字符串从PHP传递给JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,我也找不到正确答案。



我有一个PHP脚本,从数据库中读取人的姓名,并显示他们在桌子里在每一行中,我将该人的名称超链接到一个JavaScript函数,该函数显示该人的名称作为标题的弹出式窗口 - PHP代码和结果的HTML如下所示:

  $ name = $ results ['name']; 
echo'< a href =javascript:void(0)onclick =popup(this,'\'。$ name。'\')>'。 $名称。 < / A>;

使用html:

 < a href =javascript:void(0)onclick =popup(this,'Steve Smith')> Steve Smith< / a> 

一切正常,除非我打了一个连字符的名称,例如Bryce D Arnoud导致撇号打破了html。为了解决这个问题,我使用PHP addslashes()函数,导致以下代码& html:

  $ name = $ results ['name']; 
echo'< a href =javascript:void(0)onclick =popup(this,'\。addslashes($ name)。'\')>'。 $名称。 < / A>;

使用html:

 < a href =javascript:void(0)onclick =popup(this,'Bryce D \Arnoud')> Bryce D'Arnoud< / a& 

一切功能都很好,但由于某种原因,JavaScript不会删除转义字符,而在popup()函数,如果我提醒名字,我看到Bryce D \'Arnoud

  function popup(t, name){
alert(name);
}

有关我出现错误的建议?

解决方案

首先,如果您的标记中未混合JavaScript,那么生活会更容易。相反,请使用PHP格式的语义标记,并使用JavaScript访问数据:

  $ name = $ results ['name']; 
echo'< a href =#class =popupdata-name ='。$ name。'>'。 $名称。 < / A>;

然后访问该值(我使用jQuery为这个简洁):

$ b点击(function(e){
e.preventDefault();

alert($(this).attr('data-name'));
});

在本机JS(我想)中:

  document.getElementsByClassName('popup')。onclick = function(e){
e = e || window.event;

e.preventDefault();

alert(this.getAttribute('data-name'));
};


A simple problem that I just can't find the 'right' answer too.

I have a PHP script that reads people's names from a database, and display them in a table. In each row, I have hyperlinked the person's name to a JavaScript function that displays a popup with the person's name as the title - the PHP code and resulting html follows:

$name = $results['name'];
echo '<a href="javascript:void(0)" onclick="popup(this, '\' . $name . '\')">' . $name . '</a>';

With html:

<a href="javascript:void(0)" onclick="popup(this, 'Steve Smith')">Steve Smith</a>

Everything works fine, except when I hit a name that has a hyphen, such as "Bryce D'Arnoud" which results in the apostrophe breaking the html. To get around this issue, I am using the PHP addslashes() function, resulting in the following code & html:

$name = $results['name'];
echo '<a href="javascript:void(0)" onclick="popup(this, '\' . addslashes($name) . '\')">' . $name . '</a>';

With html:

<a href="javascript:void(0)" onclick="popup(this, 'Bryce D\'Arnoud')">Bryce D'Arnoud</a>

Everything functions fine, but for some reason, the JavaScript is not removing the escape character, and in the popup() function, if I alert out name, I see "Bryce D\'Arnoud"

function popup(t, name) {
  alert(name);
}

Any suggestions as to where I am going wrong?

解决方案

First, life would be a lot easier if you didn't mix JavaScript in your markup. Instead, have PHP form semantic markup, and access the data with JavaScript:

$name = $results['name'];
echo '<a href="#" class="popup" data-name="' . $name . '">' . $name . '</a>';

And then access the value (am using jQuery for this for terseness):

$('.popup').click(function(e) {
    e.preventDefault();

    alert($(this).attr('data-name'));
});

And in native JS (I think):

document.getElementsByClassName('popup').onclick = function (e) {
    e = e || window.event;

    e.preventDefault();

    alert(this.getAttribute('data-name'));
};

这篇关于将带有撇号的字符串从PHP传递给JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆