简体   繁体   中英

AJAX won't get PHP - echo

So I'm trying to build a pretty simple website, to learn basic webdesign. It's just a random quote generator - you click on a button and it prints some famous quote. Anyway, I try to use ajax to get the quote, so I use this function:

function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }

    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("quote").innerHTML=xmlhttp.responseText;
            }
    }
    xmlhttp.open("GET","RandomQuote.php",true)
    xmlhttp.send();
}

but no matter what I type into RandomQuote.php, even if it's something like :

<?php 
 echo 'Hello, world';
 ?>

nothing shows up in the quote "div', it just becomes blank. I really have no idea what's the problem here. Thanks for the help!

Well, I'm not sure about much, but are you calling your function? You put your ajax inside loadXMLDoc() , so you probably have to call it. Another way is to put your ajax into an addEventListener so when a user clicks on something, it'll execute. If that's not the problem, try making sure your element in your html page with the id of "quote" is spelled correctly. Sometimes the latter scenario is always the problem for me.

    $.get( "RandomQuote.php", function( data ) {
      $( "#quote" ).html( data );
      console.log( "Load was performed." );
    });

Don't forget to include jQuery.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

This is my ajax function, try to use it:

/**
 * @function ajax               Send ajax-request with post
 * @param  {string}   xmlpage   Target url
 * @param  {string}   data      Post parameters (like param1=val1&param2=val2...)
 * @param  {Function} callback
 * @return {null}    
 */
function ajax(xmlpage, data, callback)
{ 
    var xmlh = null;
    if(window.XMLHttpRequest)
        xmlh = new XMLHttpRequest();
    else
        try
        { 
            xmlh = new ActiveXObject("Msxml2.XMLHTTP"); 
        }
        catch(ex) 
        { 
            xmlh = new ActiveXObject("Microsoft.XMLHTTP"); 
        }
    if(xmlh)
    {
        xmlh.open("post", xmlpage, true);
        xmlh.onreadystatechange = function(x) 
                            { 
                                if(xmlh.readyState==4 && typeof callback !== 'undefined') 
                                    callback(xmlh.responseText); 
                            }
        xmlh.setRequestHeader("Accept-Language","ru, en");
        xmlh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlh.send(data);
    }
}

Following test files using your code from the question. It is working perfectly. Either you are missing something or its a browser issue. I tested on Mozilla. To be sure of browser independent code use jQuery for Ajax call

test.html

<html>
<script>
function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }

    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("quote").innerHTML=xmlhttp.responseText;
            }
    }
    xmlhttp.open("GET","RandomQuote.php",true)
    xmlhttp.send();
}
</script>
<div id="quote"></div>
<input type="button" value="Click Me" onclick="loadXMLDoc()"/>
</html>

RandomQuote.php

<?php

echo 'hi';

Update: jQuery version

<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
function loadXMLDoc()
{
    $.get( "RandomQuote.php", function( data ) {
      $( "#quote" ).html( data );
    });

}
</script>
<div id="quote"></div>
<input type="button" value="Click Me" onclick="loadXMLDoc()"/>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM