0

I want to pass value of a variable from PHP file to use it as an argument into JavaScript function. E.g:

PHP:

$passed = "passed";
$failed = "failed";

$name = $_POST['name'];

if($name == "lekz"){
     echo $passed; // this variable will pass to js file
}
else{
    echo $failed; // this variable will pass to js file
}

JS:

function trace(){

    var con = "passed"

    if($passed == con){
        alert("passed");
    }
    else{
        alert("failed");
    }
}

In what way can I pass variables from PHP into JS function?

halfer
  • 19,824
  • 17
  • 99
  • 186
AlexJaa
  • 389
  • 7
  • 20
  • Is the JS function in the PHP file? – Barmar Jul 29 '14 at 08:06
  • Nope. A separate file sir. – AlexJaa Jul 29 '14 at 08:10
  • Then you'll probably have to use the AJAX method in the linked question. – Barmar Jul 29 '14 at 08:11
  • Then you'll probably need to do an AJAX request. http://api.jquery.com/jQuery.ajax/ – somethinghere Jul 29 '14 at 08:11
  • @Barmar, actually sir I've been troubled from my past question so I believe I can find it easier to ask a new question. BTW here's my past question sir maybe you can help me into this http://stackoverflow.com/questions/25008625/check-database-connection-after-button-is-click – AlexJaa Jul 29 '14 at 08:18
  • Looks like you already have good answers there. If you're having trouble understanding them, you need to study how AJAX works. SO is not a substitute for learning how to program. – Barmar Jul 29 '14 at 08:24
  • I guess so sir. Maybe I'm just so stressed and have to take a break :) Thank you btw :D – AlexJaa Jul 29 '14 at 08:27

0 Answers0