0
<body onload="check(<?php echo($type);?>,<?php echo($bool);?>)">
    #code...
</body>

function check(var1,var2)
{
   alert(var1);
   if (var2 == true)
   {
      edit(var1);   
   };
}
# var1 is a string
# var2 is a true/false boolean

# var2 works just fine, passes through the FALSE/TRUE value without problems
# var1 on the other hand is the problem.

Hello everyone, i'm trying to pass PHP a variable into my javascript function. the variable $type isset to "email", like this;

$data['type'] = "email";

But for some reason the javascript function automatically picks the value of my email input instead of the value that i assigned to the $type variable.

If i set $type to something else, like this;

$data['type'] = "random";

Note: I've been trying to alert(); the value just to make sure it's the correct value.

So if i try to alert(type); from within the javascript function and 'type' is set to anything that DOESN'T exist in my form, nothing happens.

If i set 'type' do something that DOES exist in my form(e.g. password, email) it will automatically alert(); the value from my form inputs, and not the value i assigned to $data['type'] in my PHP function.

Anyone got a clue what the problem is?

2
  • Quote-escape it and put it in a JavaScript string literal if that is what it is supposed to become? Commented Oct 7, 2013 at 17:06
  • Never compare a boolean against true! Commented Oct 7, 2013 at 18:44

1 Answer 1

1

Try adding quotes around var1 Like this :

<body onload="check('<?php echo($type);?>',<?php echo($bool);?>)">
Sign up to request clarification or add additional context in comments.

1 Comment

But make sure to escape apostrophes and backslashes (for JS) and quotes (for HTML)!

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.