Felipe
Felipe

Reputation: 5196

Passing a PHP variable as a parameter using HTML onClick

I looked through many similar examples, but I could not make this work.

So, I have this:

echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
      <button class="btnTrack" onClick="trackIt(' . $name . ')" >Track It!</button></td>');

That code is inside a for, so the variable $name will be different depending where I am at.

I am not trying to make things complicates, so first, I am just trying to pass that parameter to the function trackIt (I actually need to pass 2 of them.)

Then, I have a simple script (just to see if it will work):

<script>
//After you click on Track It

function trackIt(param) 
{
   alert("Hi!");
   alert(param);
}

</script>

However, it does not work.

If my onClick function is just onClick="trackIt()", then it works fine and I can alert "Hi!" by removing the parameter.

Thanks for the help! =]

Upvotes: 2

Views: 19030

Answers (4)

Mr Talha
Mr Talha

Reputation: 825

For passing multiple parameters you can cast the string by concatenating it with the ASCII value like, for single quotes we can use &#39

var str= "&#39;"+ str+ "&#39;";

This works for me.

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50787

The reason this doesn't work is due to the fact that you're passing an unencapsulated string as an argument in your HTML, however, it's being interpreted as a javascript variable, such as:

Uncaught ReferenceError: ValueOf$name is not defined

' Or if the variable contains spaces or special characters:

 Uncaught SyntaxError: Unexpected identifier

To resolve this, you should encapsulate your string with quotations.

"trackIt(\'' . $name . '\')"

If you're using an integer, string encapsulation is not required.

jsFiddle examples

Upvotes: 2

Sampath Liyanage
Sampath Liyanage

Reputation: 4896

Try this..

echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
  <button class="btnTrack" onclick="trackIt(\'' . $name . '\')" >Track It!</button></td>');

Note: you have to use your way if the input parameter is a numeric value..


You are printing HTML as,

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
<button class="btnTrack" onclick="trackIt(test)" >Track It!</button></td>

But because the function trackIt needs a string as the input parameter, you have to print this..

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
<button class="btnTrack" onclick="trackIt('test')" >Track It!</button></td>

As you are using ' as boundaries to define strings in PHP, you have to escape it using \' in order to make ' character part of the string.

Upvotes: 10

Marco Mura
Marco Mura

Reputation: 582

Even if other answers are true... If you do not need to really print all that html use php differently

echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
      <button class="btnTrack" onClick="trackIt(' . $name . ')" >Track It!</button></td>');

it can become:

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
          <button class="btnTrack" onClick="trackIt(<?php print $name;?>)" >Track It!</button></td>

Use php only when needed :)

Upvotes: -3

Related Questions