dlebowski Posted November 15, 2010 Share Posted November 15, 2010 Hi. Using the example below, I want to be able to make one call to a PHP file, but have it update multiple DIVS. For example, if I have <div id="txtHint"</div> and <div id=""txtHint2"></div>, I want to be able to make one AJAX request to a PHP file, grab two values out of MYSQL and update multiple DIV's. Does anyone know if this is possible? Thank you for your time! HTML: <html> <head> <script type="text/javascript"> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } 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("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> <select name="users" onchange="showUser(this.value)"> <option value="">Select a person:</option> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Glenn Quagmire</option> <option value="4">Joseph Swanson</option> </select> </form> <br /> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html> PHP: <?php $q=$_GET["q"]; $con = mysql_connect('localhost', 'peter', 'abc123'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ajax_demo", $con); $sql="SELECT * FROM user WHERE id = '".$q."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Hometown</th> <th>Job</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/218757-ajax-one-php-call-updated-multiple-divs/ Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 If you still haven't figured it out: Yes, it is possible. You will just have to ensure that your client/server side script requests/outputs the correct number of values. You could send different values on separate lines, or if you data is more complex, a possible solutions would be to use JSON. Link to comment https://forums.phpfreaks.com/topic/218757-ajax-one-php-call-updated-multiple-divs/#findComment-1136462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.