0

I'm trying to load content from a php file which name is "include.php" to a in another php file which name is "index.php". But the loading does not work. The code is as below:

The file: index.php

<header>
<script type="text/javascript">
    function load(){
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }else{
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }

        xmlhttp.onreadystatechage = function (){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                document.getElementById(adiv).innerHTML = xmlhttp.responseText;
            }
       }

        xmlhttp.open('GET', 'include.php', true);
        xmlhttp.send();
    }

    </script>
</head>
<body>
    <input type="submit" value="Submit" onclick="load();">
    <div id="adiv"></div>
</body>

The File: include.php

<?php
    echo 'Hello!';
?>

Thanks.

2
  • Go to Inspect element -> network tab -> what can you see there? Commented Jun 21, 2015 at 6:59
  • There is a list of my three files: "index.php", "style.css", and"include.inc.php". Method of all these three files is "GET". STATUS for "index.php" and "include.inc.php" is "OK". STATUS of "style.css" is "Not Modified." Commented Jun 21, 2015 at 20:35

3 Answers 3

1

If what you are doing is the way you describe then this is simpler:

<header>
<?php include("include.php") ?>
</head>
<body>
    <input type="submit" value="Submit" onclick="load();">
    <div id="adiv"></div>
</body>

If you want to get result from include.php into JavaScript then you'll probably be better using ajax.

By the way, if you are planning a "universal header" for all your PHP files, you don't need to echo it just write it as normal HTML with any necessary PHP tags

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

2 Comments

Hey if you like the answer can you give a check mark to signify this question has been answered?
Thanks Michael. What I want to do is loading some content from another file to a div without refresh the page. In this case, when I click the "Submit" button, the content on another file should be loaded under the "Submit" button but no page refresh.
0

Should it not be document.getElementById("adiv").innerHTML = xmlhttp.responseText;? Notice the quotes.

2 Comments

Thanks, and I correct this error, but still does not work.
It works now. I changed xmlhttp.onReadyStateChange = function (){ back to xmlhttp.onreadystatechange & corrected getElementById("adiv"). Thanks all of your helps. @Nitesh Kumar Anand, @Kirs Sudh, @Michael Christensen, @ Kunal Pradhan
0

....xmlhttp.onreadystatechage = function (){ if(xmlhttp.readyState == 4 && xmlhttp.st...

Check the spelling onReadyStateChange.

Comments

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.