1

i want to pass a php variable to javascript

on php(getStatus.php) i'm using json_encode like this

$resultFin = array();
if (strpos($status,'Scan is complete') === true){
$resultFin[] = 1;
}
else{ 
    $resultFin[] = 0;
}

echo json_encode($resultFin);
echo "$status";

and i want get resultFin value on javascript so i wrote below code

echo '<script type="text/javascript">
$(document).ready(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:' . "$rxss" . ',sxss:' . "$sxss" . ',sqli:' . "$sqli" . ',basqli:' . "$basqli" . ',autoc:' . "$autoc" . ',idor:' . "$idor" . ',dirlist:' . "$dirlist" . ',bannerdis:' . "$bannerdis" . ',sslcert:' . "$sslcert" . ',unredir:' . "$unredir" . ',clamav:' . "$clamav" . '}, function(data){$("#status").html(data)});
     var refreshId = setInterval(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:' . "$rxss" . ',sxss:' . "$sxss" . ',sqli:' . "$sqli" . ',basqli:' . "$basqli" . ',autoc:' . "$autoc" . ',idor:' . "$idor" . ',dirlist:' . "$dirlist" . ',bannerdis:' . "$bannerdis" . ',sslcert:' . "$sslcert" . ',unredir:' . "$unredir" . ',clamav:' . "$clamav" . '}, function(data){$("#status").html(data)});
console.log("dfdfdf");
$.getJSON("scanner/getStatus.php", function(data){
        var fini;
        fini = data;
        console.log(fini);
        if( fini == 1){
            clearInterval(refreshId);
            }
        });
}, 500);
$.ajaxSetup({ cache: false });
});</script>';

i have checked it using brakepoint console.log("dfdfd");

but not getting into getJSON method and don't get resultFin value

how to fix this code?

i want resultFin value using in javascript

2
  • Set value of $resultFin in HTML hidden input and access it on javascript side Commented Aug 17, 2017 at 6:12
  • 1
    When you echo status after you echo your json encoded $resultFin, you are essentially corrupting your json. Commented Aug 17, 2017 at 6:23

1 Answer 1

1

Your getStatus.php should look like:

$resultFin = array();
if (strpos($status,'Scan is complete') === true){
$resultFin['scan_completed'] = true;
}
else{ 
    $resultFin['scan_completed'] = false;
}

echo json_encode($resultFin);

So on frontend you can use fini.scan_completed which will be true or false.

Sign up to request clarification or add additional context in comments.

1 Comment

or just $resultFin = ['scan_completed' => strpos($status,'Scan is complete') === true]

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.