-2

I have tried all the post of stack overflow and from google but none of those seem to work for me. This is my php code:

echo $firstPar;
echo $secondPar;
$tab_str.="<td width=\"20%\"><a href=\"#\" onclick = \"functionCall(".$firstPar.", ".$secondPar.")\" class=\"previous round\">&#8249;</a>";

This is the Javascript code:

function functionCall(firstPar, secondPar) {
    alert(firstPar);
    alert(secondPar);
}

If I am passing string values directly on the php call rather than passing the variables, I am getting the values inside the function. But the variable values are coming as null. Whereas the echo displays the variable values.

4
  • Really all the posts? More seriously, you should check and post what is the output of the PHP code. Commented Jul 24, 2018 at 13:08
  • 1
    You'll need to quote the variables inside your onClick string. Otherwise, it will think it's trying to pass variables instead of strings. You can verify what javascript sees by looking at the source code in your browser. Commented Jul 24, 2018 at 13:09
  • Ok. I will try that. Commented Jul 24, 2018 at 13:11
  • 1
    The way you are quoting your string is quite confusing. It may help you find your issue if you replace the outer quotes with 's and then you can leave the inner "s without escaping Commented Jul 24, 2018 at 13:12

2 Answers 2

1

There should be quotes around parameters. Try using this:

$tab_str .= "<td width=\"20%\"><a href=\"#\" onclick = \"functionCall('" . $firstPar . "', '" . $secondPar . "')\" class=\"previous round\">&#8249;</a>";
Sign up to request clarification or add additional context in comments.

Comments

0

Try like this

echo $firstPar;
echo $secondPar;
$tab_str .= '<td width="20%"><a href="#" onclick = "functionCall(\'' . addslashes($firstPar) . '\', \'' . addslashes($secondPar) . '\')" class="previous round">&#8249;</a>';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.