0

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.

Edited by pyTony: fixed formatting

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by jhaque
0

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

Edited by chintan@dani: n/a

0

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);
}

Edited by pyTony: fixed formatting

0

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();

}

Edited by chintan@dani: n/a

0

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

but still its not working.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.