0

I have a JavaScript function that populates and shows a div when a user hovers over something. I've found out that if the input that I pass to the function contains a blank line, it won't work. Is there an easy way to encode my variables in PHP so it can pass safely?

Javascript:

function showPlay(id, info)
{
    var playID = "my_play_div" + id;
    var snippetID = "post_snippet" + id;

    document.getElementById(playID).style.visibility="visible";
    document.getElementById(snippetID).innerHTML = info;
}

I've tried using $info = htmlentities($info); on my content and $info = mysql_real_escape_string($info); but neither of these got rid of the blank lines, just the quotes (which I need to get rid of as well).

Anyone have a better solution?

3
  • presumably your string contains a newline character - you need to find it and replace it with nothing. Do you know how to do a search and replace in php? Since this is not a "bad" character, the things you have tried so far won't get rid of it... Commented Feb 26, 2013 at 22:33
  • How is the PHP variable $info getting into the Javascript variable info? Commented Feb 26, 2013 at 22:43
  • I echo it to get it into the Javascript variable. You can view my jsfiddle and see that only the Fall Out Boy post works (because this doesn't contain any new lines or anything, I think) jsfiddle.net/Wg9fQ Commented Feb 26, 2013 at 22:54

2 Answers 2

3

I think you need json_encode, echo json_encode($info);

Sign up to request clarification or add additional context in comments.

1 Comment

This along with some htmlentities() calls seemed to fix it.
0

The php trim function will do what you need: see http://php.net/manual/en/function.trim.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.