admin管理员组

文章数量:1318156

How do I pass 2 PHP variables in a javascript function?

This one is working

 echo '<button onClick = "count('.$increment.')">'.$counter.' </button>';

but when i do this

  echo '<button onClick = "count('.$increment.','.$pass.')">'.$counter.' </button>';

it does not work, what is the problem?

By the way these are the variables:

    $increment=5;
    $pass="sdfgd";

How do I pass 2 PHP variables in a javascript function?

This one is working

 echo '<button onClick = "count('.$increment.')">'.$counter.' </button>';

but when i do this

  echo '<button onClick = "count('.$increment.','.$pass.')">'.$counter.' </button>';

it does not work, what is the problem?

By the way these are the variables:

    $increment=5;
    $pass="sdfgd";
Share Improve this question edited Feb 14, 2013 at 11:52 Tjoene 3509 silver badges18 bronze badges asked Feb 14, 2013 at 11:28 Andre malupetAndre malupet 1113 silver badges12 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

Try this dude......

<button onClick = "count('<?php echo $increment ?>','<?php echo $pass ?>')"><?php echo $counter ?></button>
echo '<button onClick = "count('.$increment.',\''.$pass.'\')">'.$counter.' </button>';

If $pass contains exactly "sdfgd" and with exactly I mean double quotes includes, this isn't a valid statement anymore, because your parser will find double quotes "too early" and them will close the onClick event attribute

After variable expansion:

echo '<button onClick = "count('5','"sdfgd"')">'.$counter.' </button>';
-------------------------------------^

Take a look to the arrow

Edit

However, if you use a tool like firebug (if you run firefox), you can debug your code easily

The generated HTML code should look like this:

<button onClick = "count(5,sdfgd)">5 </button>

The variable sdfgd is most likely undefined, therefore undefined gets passed to your function.

If you want to pass a string you have to use quotes, so that the generated HTML looks like this:

<button onClick = "count(5,'sdfgd')">5 </button>

本文标签: How do I pass 2 PHP variables in a javascript functionStack Overflow