Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
How to return variable from php to javascript

I have this part of javascript, How do I return res.success and res.reload from php?

if (res.success) {
                    NioApp.Toast(res.success, 'success');
                    if (res.reload) {
                        setTimeout(function() {
                            location.reload();
                        }, 900);
                    }
                } 

I tried $return ['res' => 'success'] but didn't work for me.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The question is not that clear, But I am just trying to solve it. If you want to get the value from PHP to js, then either you need to use an async API call to the server to get the value. or you need to echo out the value to some HTML hidden elements and use javascript to get the value to a variable using getElement.But if you want to send the value to the server then use the get or post method.

about 4 years ago · Juan Pablo Isaza Report

0

I guess by 'res' you meant ajax response here, if so, you need to echo out json from php file.

PHP

<?
 $result = array("success" => "success", "reload" => "true");
 echo json_encode($result);
?>

Script

$.post("someFile.php", {//somedata},
  function(data, status){
  if(status == "success"){
  res = JSON.parse(data);
  //now here is your script
  if (res.success) {
                    NioApp.Toast(res.success, 'success');
                    if (res.reload) {
                        setTimeout(function() {
                            location.reload();
                        }, 900);
                    }
                } 
}
}
)

Comment down for any queries.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!