Hello everyone, i am new to ajax and i am trying to get some data from database using php and ajax. I am facing a problem and i can't find any solution. When i trigger the ajax function, it not showing what i wanted. please check the code bellow and give some idea.

cheers,
jhaque.

my xmlHttpRequest function.

//function that creates the xmlrequest object.
var xmlHttp;
function makeRequest(url) {

    if(window.XMLHttpRequest) {

        xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {

        xmlHttp = new XMLHttpRequest();
    }
    else
    alert("what is wrong now");
    if(xmlHttp) {

        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
        xmlHttp.onreadystatechange = show_item;
    }



}//End of the function.

my php file-----

<?php
include('connect.php');

if($connection !=null){

//Selecting the database.
mysql_select_db('modern_tandoori');
$special_query="select menu_item from special_menu;";
$special_result = mysql_query($special_query);

while($special_row = mysql_fetch_array($special_result)){


        echo($special_row['menu_item']."<br/>");


}//End of the while loop.

}
else
    echo("Connection failed!!!");

mysql_free_result($special_result);

mysql_close($connection);
?>

Another php file where i am calling the javascript function for ajax:

<?php

include('connect.php');

if($connection !=null){

    //Selecting the database.
mysql_select_db('modern_tandoori');

    //Query
    $mysqery = "Select offer_name from special_offer;";

    $myresult = mysql_query($mysqery);
    $row = mysql_fetch_array($myresult);

    echo("<a onclick = \"show_item()\" href=\"\">".$row['offer_name']."</a>");
    echo("<br/>");


    echo("<div id=\"special_dis\">");

    echo("</div>");





}else
    echo("Couldn't connected with the server, Contact admin");

mysql_free_result($myresult);

mysql_close($connection);

?>

Finally my javascript function where i am trying to display and responseText:

function show_item() {
alert("hi");
makeRequest("spcial_menu.php");

    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {

        document.getElementById("special_dis").innerHTML = xmlHttp.responseText;

        alert("hi");

    }
    else

    alert("Again something wrong with xml status"+xmlHttp.status);
}

please help me.

Dani AI

Generated

A few focused things to check — this will find the cause quickly and fix the status == 0 and “nothing shows” symptoms.

  • The most likely immediate cause: the link you echo uses href="". Clicking that will navigate (and often abort the XHR), producing status == 0. Use a non-navigating control (prevent the default action, return false from the onclick, or use a button) so the request can complete without the browser reloading the page. This ties to what suggested about how you wire the request handler.

  • Look for a JavaScript runtime error that stops execution. A typo like document.getElemenyById will throw and halt the handler before the DOM update. Open the Console (DevTools) and confirm there are no exceptions when you click.

  • Verify the request actually went out and what the server returned: use the Network panel to observe the XHR entry, response body, URL, and final status code. If there is no request entry, the click is causing navigation/abort or the handler never runs.

  • Confirm you are testing over HTTP(S) and not file://. XMLHttpRequest.status is 0 for aborted requests, network errors, or when using the file protocol. Also make sure the requested filename/path is spelled correctly and reachable (try opening the PHP file directly in the browser to see raw output and any PHP warnings).

  • Check lifecycle/order and scope: the XHR must be created in scope the handler can access, onreadystatechange should be set so the callback runs as the state changes, and the DOM element ID must match exactly. Using the Network/Console logs is faster and less intrusive than alerts.

For precise behavior of readyState and what a status of 0 means, see the MDN docs on readyState and status:

Applying those checks will reveal whether the request is aborted by navigation, blocked by a JS error, or returning a server error. Correcting the empty href, the getElementById typo, and confirming the PHP output usually resolves this exact problem.

Recommended Answers

All 5 Replies

you are doing wrong while creating xmlHttpRequest. for IE you have to create it like
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

Again you are taking responce from xmlhttp.onreadystatechange which is not correct. ajax will give you responce for xmlhttp.readyState=2 or 4 in xmlhttp.responseText.
Take a Reference from here.
Ajax PHP

Thanks a lot Chintandani. But i made the changes and yet still no result. Here is my latest code.----

//function that creates the xmlrequest object.
var xmlHttp;
function createRequest() {

    if(window.XMLHttpRequest) {

        xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {

        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    alert("what is wrong now");


}//End of the function.


function makeRequest() {
createRequest();
xmlHttp.onreadystatechange = show_item;
xmlHttp.open("GET", "special_menu.php", true);
xmlHttp.send(null);

}


function show_item() {
alert("hi");
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            document.getElemenyById("special_dis").innerHTML = xmlHttp.responseText;

        }

    }
    else
    alert("there is a problem with this request\t:"+xmlHttp.status);
}

I think you are not calling function properly and what you are passing to special_menu.php?
You will get responce from that page in responseText
try this

function makeRequest() {
createRequest();
xmlHttp.open("GET", "special_menu.php", true);
xmlHttp.onreadystatechange = show_item;

xmlHttp.send();

}

sorry mate, i followed your suggestion and change onreadystatechange = show_item();

but still its not working.

Now, i think i have made some progress here. after following chintandani's suggestion, when i am running ajax i am getting one error message pupped out says:

"there is a problem with this request: 0".

its showing 0 because i alert xmlHttp.status.

Any suggestion? please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.