skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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);
                        }
                    } 
    }
    }
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search