如何将 PHP 字符串传递到 Javascript 函数调用中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13905313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass a PHP string into a Javascript function call?
提问by Brecon
Possible Duplicate:
Pass a PHP string to a Javascript variable (and escape newlines)
So, essentially I am trying to pass a string from a PHP page as an argument for a javascript function. The PHP is included in the page that the script is on, but they are in two separate files.
所以,本质上我试图从 PHP 页面传递一个字符串作为 javascript 函数的参数。PHP 包含在脚本所在的页面中,但它们位于两个单独的文件中。
However, whenever I try to pass a string, it stops the javascript from running. I can pass PHP integers, but not strings. Is it possible to fix this? I've been looking around the internet for what might be causing my error, but I can't find anything. Any help would be greatly appreciated.
但是,每当我尝试传递字符串时,它都会停止运行 javascript。我可以传递 PHP 整数,但不能传递字符串。有没有可能解决这个问题?我一直在互联网上寻找可能导致我出错的原因,但我找不到任何东西。任何帮助将不胜感激。
This is the PHP and HTML. The function runs when submit is clicked.
这是 PHP 和 HTML。该函数在单击提交时运行。
$comment = "7b";
echo"
<table>
<p><input type='text' id='newPostComment' value='' placeholder='WRITE YOUR COMMENT HERE...' size='35'/><input type='submit' value='UPLOAD' onclick='uploadPostComment($parentID, $comment);fetchComment()'/></p>
</table>
This is the javascript function.
这是javascript函数。
function uploadPostComment(PostCID, Comment){
var PostCommentData = "fsPostID="+PostCID;
PostCommentData += "&fsPostComment="+Comment;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "uploadPostComment.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", PostCommentData.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
alert("Message Posted");
}
else
{
alert("Oh no, an error occured2");
}
}
};
xmlHttp.send(PostCommentData);}
回答by cryptic ツ
echo"
<table>
<p><input type='text' id='newPostComment' value='' placeholder='WRITE YOUR COMMENT HERE...' size='35'/><input type='submit' value='UPLOAD' onclick='uploadPostComment(\"$parentID\", \"" . str_replace('"', '\"', $comment) . "\");fetchComment()'/></p>
</table>
You need to quote the fields. If your field is an integer it does not need to be quoted, if it is a string it needs to be. I quoted both because I don't know if parentID is a string id or a numeric id. Do what your application needs of course.
您需要引用字段。如果您的字段是整数,则不需要引用,如果是字符串,则需要引用。我引用了两者,因为我不知道 parentID 是字符串 id 还是数字 id。做你的应用程序当然需要什么。
Update: in regards to Michael's comment about breaking if comment contains a quote. We now escape all double quotes in $comment to prevent that.
更新:关于迈克尔关于打破如果评论包含引用的评论。我们现在转义 $comment 中的所有双引号以防止这种情况发生。
回答by Eugen Rieck
<input type='submit' value='UPLOAD' onclick='uploadPostComment($parentID, $comment);fetchComment()'/>
is your culprit, especially uploadPostComment($parentID, $comment);
- if $comment is a string, you need to put quotes in place: uploadPostComment($parentID, \"$comment\");
是你的罪魁祸首,尤其是uploadPostComment($parentID, $comment);
- 如果 $comment 是一个字符串,你需要把引号放在适当的位置:uploadPostComment($parentID, \"$comment\");
回答by Viral Patel
Wrap the string in escaped double quotes like \"$comment\"
将字符串包裹在转义的双引号中,例如 \"$comment\"
$comment = "7b";
echo"
<table>
<p><input type='text' id='newPostComment' value='' placeholder='WRITE YOUR COMMENT HERE...' size='35'/><input type='submit' value='UPLOAD' onclick='uploadPostComment(\"$parentID\", \"$comment\");fetchComment()'/></p>
</table>