2

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

4 Answers 4

4

Try this dude......

<button onClick = "count('<?php echo $increment ?>','<?php echo $pass ?>')"><?php echo $counter ?></button>
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

wow this works also..wow didnt know it was that easy thanks man
1

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

3 Comments

so does that mean that currently there is no way i can input a string? how do i input a string defined variable like $pass.
@Andremalupet Take a look here
ill try this one..this is same from jsonencode right?uhmm..its weird that firebug is actually returning the variable value as undefined yet it has a variable and a value..sorry for asking..but what does that mean..thanks anyway men..you are a big help.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.