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.