简体   繁体   中英

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

试试这个家伙......

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM