AL123 Posted November 27, 2009 Share Posted November 27, 2009 I integrated some ajax into my php site. I took two examples from w3schools. One uses an xml file to display some CD info, the other goes to the data base. They should display in their own place on the page but they replace each other. Also my return from the database prints twice. here is the php: <?php $r=$_GET["r"]; $dbh=new PDO('mysql:host=localhost;dbname=///','///','///'); if (!$dbh) { die('Could not connect: ' . mysql_error()); } function get_quote($r) { global $dbh; $sql="SELECT name, quote FROM tblQuote WHERE id = '".$r."'"; $sth = $dbh->prepare($sql); $sth->bindValue(':r', $r, PDO::PARAM_INT);//(':q', $q, PDO::PARAM_INT) $sth-> execute(); $row = $sth->fetch(PDO::FETCH_ASSOC); return $row; } $display = get_quote($r); //print_r($display); echo "<table border='1'> <tr> <th>name</th> <th>quote</th> </tr>"; //while($row = mysql_fetch_array($display)) //$row = mysql_fetch_array($result) $key => //foreach($display as $value) foreach($display as $key => $value) { echo "<tr>"; echo "<td>" . $display['name'] . "</td>"; echo "<td>" . $display['quote'] . "</td>"; echo "</tr>"; } echo "</table>"; Here is a link to the site, It's a work in progress: http://nuke.empireindustry.com/ Thanks, AL123 Quote Link to comment Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 post your ajax Quote Link to comment Share on other sites More sharing options...
AL123 Posted November 27, 2009 Author Share Posted November 27, 2009 Here is the js that goes with it, along with the CD script. for the Quote: var xmlhttp; function showUser(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="processReq/getuser.php"; url=url+"?r="+str;//?q= url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint2").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } For the CD info: var xmlhttp; function showCD(str) // solve the problem of creating different XMLHTTP objects for different browsers. { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="processReq/getcd.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } Quote Link to comment Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 You have two functions called stateChanged(). Either A) rename them to something unique, or B(best option) pass in the div id to the stateChanged function. Also don't use <br/> to space out DIV elements. Quote Link to comment Share on other sites More sharing options...
AL123 Posted November 27, 2009 Author Share Posted November 27, 2009 I tried to pass the div id this way: function stateChanged(document.getElementById("txtHint")) [<div id="txtHint">] and nothing worked. So I tried renaming the function to stateChanged1, and that flip-flopped the situation. Bare with me I'm new. Thanks, AL123 Quote Link to comment Share on other sites More sharing options...
AL123 Posted November 27, 2009 Author Share Posted November 27, 2009 I should not have put: [<div id="txtHint">] after the function in my post. It is not in my code. AL123 Quote Link to comment Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 I tried to pass the div id this way: function stateChanged(document.getElementById("txtHint")) [<div id="txtHint">] and nothing worked. So I tried renaming the function to stateChanged1, and that flip-flopped the situation. Bare with me I'm new. Thanks, AL123 No. function stateChanged(divID) { if (xmlhttp.readyState==4) { document.getElementById(divID).innerHTML=xmlhttp.responseText; } } Call it like this: xmlhttp.onreadystatechange=stateChanged("txtHint"); //OR xmlhttp.onreadystatechange=stateChanged("txtHint2"); Quote Link to comment Share on other sites More sharing options...
AL123 Posted November 27, 2009 Author Share Posted November 27, 2009 This is what I did function stateChanged() { if (xmlhttp.readyState==4) { xmlhttp.onreadystatechange = stateChanged("txtHint2"); //document.getElementById("txtHint2").innerHTML=xmlhttp.responseText; } } What happens to - innerHTML=xmlhttp.responseText; ? Quote Link to comment Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 I gave you the answer in my last post. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.