Skip to content Skip to sidebar Skip to footer

How To Pass Php Parameter To Javascript

Possible Duplicate: pass php variable value to javascript I have a piece of php code function formLetterTabPage($redirect_url, $letter){ $test = 123; foreach (range('A','Z')

Solution 1:

You need additional quotes in your javascript for string parameters:

echo '<li class="a" onclick="tab_click(\''.$letter.'\')">'.$letter.'</li>';

In your test case $test = 123; you are passing an integer, so the quotes are not needed.

EDIT

Output without quotes (invalid javascript):

<li class="a" onclick="tab_click(a)">a</li>

Output with quotes:

<li class="a" onclick="tab_click('a')">a</li>

Solution 2:

If you pass $test = 123 your javascript it's working because you're passing an integer value.

But your $letter it's a string and so the resulting html code it's wrong:

<li class="navi_letter_leftb" id="li_A" onclick="tab_click(A)">A</li>

You have to wrap your string between quotes ''. So it should be:

<li class="navi_letter_leftb" id="li_A" onclick="tab_click('A')">A</li>

and you should change your code to:

echo '<li class="a" onclick="tab_click(\''.$letter.'\')">'.$letter.'</li>';

Post a Comment for "How To Pass Php Parameter To Javascript"