0

Is something like this:

$html .= "<table>"
$html .= "<tr onclick=\"test(" . $phpvar . ")\"><td>test</td></tr>";
$html .= "</table>"

$rVal=array("htmltable" => $html);
echo json_encode($rVal);

JS:

success: function(data){
    $("#content").html(data.htmltable);

possible?

Everything works when no PHP var is involved, but I'm asking if there's a way to pass a PHP var like this ...

5
  • What is the value of the variable? What does the resulting JavaScript you are generating look like? Is it correct? Commented Mar 31, 2016 at 18:13
  • It's correct (functioning without error) ... the value of $phpvar is a string of a person's first and last name, and nickname ... the test(x) JS function is an alert Commented Mar 31, 2016 at 18:15
  • Don't do this. Really. Don't do this. Don't use tables for layout. Don't use inline event handlers. Don't generate JavaScript nested inside HTML nested inside PHP. It will give you a headache. The response you send to the Ajax request should be the data you care about. Generate the markup on the client. e.g. success: function(data) { $("#content").empty().append($("<button />").text("test").on("click", function() { test(data.htmltable); }); }); } Commented Mar 31, 2016 at 18:17
  • work if $phpvar is boolean or number... if is a string need to wrap beetween quotes Commented Mar 31, 2016 at 18:17
  • Thank you very much for the advice! Commented Mar 31, 2016 at 18:37

1 Answer 1

2

It's correct (functioning without error) ... the value of $phpvar is a string of a person's first and last name, and nickname

So given:

$phpvar = "John 'the Something' Smith";

The value of $html will be:

<table><tr onclick="test(John 'the Something' Smith)"><td>test</td></tr></table>

So John will be treated as a JavaScript variable name. Then you'll hit a syntax error.

You need to encode the data as a JavaScript string literal. json_encode will do that.

$phpvar = "John 'the Something' Smith";

$javascript_literal = json_encode($phpvar);

$html = "";
$html .= "<table>";
$html .= "<tr onclick=\"test(" . $javascript_literal . ")\"><td>test</td></tr>";
$html .= "</table>";

print $html;

… but that still won't work because it will give you:

<table><tr onclick="test("John 'the Something' Smith")"><td>test</td></tr></table>

… and the " will terminate the attribute value.

So you also need to encode it for HTML.

$phpvar = "John 'the Something' Smith";

$javascript_literal = json_encode($phpvar);

$html_attribute_value = htmlspecialchars($javascript_literal);

$html = "";
$html .= "<table>";
$html .= "<tr onclick=\"test(" . $html_attribute_value . ")\"><td>test</td></tr>";
$html .= "</table>";

print $html;

which will give you:

<table><tr onclick="test(&quot;John 'the Something' Smith&quot;)"><td>test</td></tr></table>

… which will work.


But that is horrible. And headache inducing. So don't do that.

  • Pass data back to the client instead of markup.
  • Use appropriate markup for the task. You want something to click on? Use a button. Not a table.

Such:

$rVal = array("plaindata" => $phpvar);
header("Content-Type: application/json");
echo json_encode($rVal);

and

success: function(data) {
    $("#content")
        .empty()
        .append(
            $("<button />")
                .text("test")
                .on("click", function() { test(data.plaindata); })
         );
}
Sign up to request clarification or add additional context in comments.

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.