3

Below is my call to a php file using ajax (index.html)

<html>
    <head>
    <title>Test Page</title>
    <script>
        function verify_data() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {         
                    //do nothing
                }
            }
            var data_id = document.getElementById("data_id").value;
            xmlhttp.open("GET", "verify.php?query=" + data_id, true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <div id="box1">Demo Content</div>
    <input type="hidden" name="data_id" id="data_id" value="23"/>
    <button onclick="verify_data();">Verify</button>
</body>

And this is my verify.php file

<?php
if(isset($_GET['query'])) {
    $data_id= $_GET['query'];
    if($data_id== 1) { //here I want to change the visibility of a html element
        echo "
        <script>
            document.getElementById('box1').style.display = 'none';
        </script>
        ";
    }
    else { //here I want to redirect user to add_data.php
        header("Location:add_data.php");
    }
}
?>

I know it is not possible to redirect user or change the content of main html page from the called page. I think it can be done by handling ajax response text,but I have no idea how to do it.

astorije
  • 2,666
  • 2
  • 27
  • 39
Brainy Prb
  • 433
  • 1
  • 9
  • 22
  • **I tried this to handle response text ,but this is not working** `if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if(xmlhttp.responseText == "redirect") { window.location('add_data.php'); } else if (xmlhttp.responseText == "change_display") { //change display code here...... } }` – Brainy Prb Aug 08 '15 at 13:09
  • You are not echoing `redirect` or `change_display` in your php code. – Sean Aug 08 '15 at 13:12
  • 1
    Maybe I don't get your point but what is the goal of making an async call (ajax) which main goal is to avoid reloading a page to get server side content and then use the returned data to reload the page?? – Lelio Faieta Aug 08 '15 at 13:15
  • @Sean I tried echoing them but it doesn't works – Brainy Prb Aug 08 '15 at 13:20
  • update your question with both the javascript code you put in the comments, and your correct php code. Without the correct code in your question, we would just be guessing what is going on. – Sean Aug 08 '15 at 13:22
  • @LelioFaieta This is not my real php file.This demonstrates my problem. But my approach is to redirect a user based on data I receive from database. – Brainy Prb Aug 08 '15 at 13:22
  • @Sean My simple question is How can I redirect a user from a PHP file which I called using ajax ? – Brainy Prb Aug 08 '15 at 13:24
  • Why you don't do it directly in server side script (php)? Ajax is there to avoid page reloading. Use the target php page for the ajax call and put there a logic for reloading. For me you are just adding an unnecessary layer of complexity – Lelio Faieta Aug 08 '15 at 13:25
  • If you call a php page using ajax, it can't redirect the calling page directly. You will need to return something to the ajax/calling page and then do a redirect there. see also http://stackoverflow.com/questions/3513971/page-redirect-with-successful-ajax-request or http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-using-jquery – Sean Aug 08 '15 at 13:26

0 Answers0