How Do I Pass A Php String Into A Javascript Function Call?
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
Solution 1:
echo"
<table><p><inputtype='text'id='newPostComment'value=''placeholder='WRITE YOUR COMMENT HERE...'size='35'/><inputtype='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.
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.
Solution 2:
<inputtype='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\");
Solution 3:
Wrap the string in escaped double quotes like \"$comment\"
$comment = "7b";
echo"
<table><p><inputtype='text'id='newPostComment'value=''placeholder='WRITE YOUR COMMENT HERE...'size='35'/><inputtype='submit'value='UPLOAD'onclick='uploadPostComment(\"$parentID\", \"$comment\");fetchComment()'/></p></table>
Post a Comment for "How Do I Pass A Php String Into A Javascript Function Call?"