Andre malupet
Andre malupet

Reputation: 111

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

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";

Upvotes: 2

Views: 6195

Answers (4)

user2071864
user2071864

Reputation: 36

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

Upvotes: 2

Venkata Krishna
Venkata Krishna

Reputation: 4305

Try this dude......

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

Upvotes: 4

DonCallisto
DonCallisto

Reputation: 29932

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

Upvotes: 1

dave
dave

Reputation: 4102

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>

Upvotes: 0

Related Questions