html - Ajax GET content from php page -
hi have table trying update call mysql database in separate php page. separate page loops through result set , builds table through series of echos. in main page trying insert echoed content div.
this kicked off user selecting option drop down box.
this separate php page. (it works fine when manually type in parameters, link between 2 pages doesn't seem work)
tablegetter.php
<?php $user_name = "rocketeermus_pr"; $password = "zuluhead2"; $database = "rocketeermus_pr"; $server = "pdb1.awardspace.com"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); echo "bonjour"; if (isset($_get['composer'])){ echo "helloooo"; if ($db_found) { echo "select * catalogue composer = '".mysql_escape_string($_get['composer'])."';"; $sql = "select * catalogue composer = '".mysql_escape_string($_get['composer'])."';"; $result = mysql_query($sql); setlocale(lc_monetary,"en_gb"); echo "<table class=\"sortable\" id=\"moder\" width=\"800\">"; echo "<th>title</th><th>composer</th><th>voicing</th><th>price</th><th></th></tr>"; while ( $db_field = mysql_fetch_assoc($result) ) { echo "hi."; echo "<tr><td>{$db_field['title']}</td><td>{$db_field['composer']}</td><td>{$db_field['voicing']}</td><td>"; echo money_format("%n", $db_field['price']); echo "</td><td> <div class=\"product\"> <input value=\"{$db_field['title']}\" class=\"product-title\" type=\"hidden\"> <input value=\"0.5\" class=\"product-weight\" type=\"hidden\"> <input value=\"{$db_field['novox']}\" class=\"googlecart-quantity\" type=\"hidden\"> <input value=\"{$db_field['price']}\" class=\"product-price\" type=\"hidden\"> <div title=\"add cart\" role=\"button\" tabindex=\"0\" class=\"googlecart-add-button\"> </div> </div> </td></tr>"; } echo "</table>"; mysql_close($db_handle); } else { print "database not found "; mysql_close($db_handle); } } ?>
and here important stuff main page:
javascript:
function getdata() { var req = getxmlhttp(); if (req) { //function called when state changed var querystring1 = ""; req.onreadystatechange = function() { //when state completed i.e 4 if (req.readystate == 4) { var ajaxsearchresults1 = document.getelementbyid("table"); ajaxsearchresults1.innerhtml = req.responsetext; // if http status "ok" if (req.status == 200) { var new1 = document.getelementbyid('composer').value; querystring1 = "?composer=" + encodeuricomponent(new1); console.log (querystring1); } else { alert("there problem while using xmlhttp:\n" + req.statustext); } } } req.open("get", "tablegetter.php" + querystring1, true); req.send(); } } function getxmlhttp() { var xmlhttp; if(window.xmlhttprequest){ //for firefox, mozilla, opera, , safari xmlhttp = new xmlhttprequest(); } else if (window.activexobject){ //for ie xmlhttp=new activexobject("microsoft.xmlhttp"); if (!xmlhttp){ xmlhttp=new activexobject("msxml2.xmlhttp"); } } return xmlhttp; }
html:
<div id="menus"> <table> <tr> <td><form action="""> <select name="composer" id ="composer" onchange="getdata();"> <?php $user_name = "***"; $password = "****"; $database = "****"; $server = "****.com"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if ($db_found) { $sql = "select distinct composer catalogue order composer"; $result = mysql_query($sql); setlocale(lc_monetary,"en_gb"); while ( $db_field = mysql_fetch_assoc($result) ) { ?> <option id="composer" onchange="getdata();" value="<?php echo $db_field['composer'];?>"> <?php echo $db_field['composer']; ?> </option> <?php } } ?> </select> </form></td> </tr> </table> </div> <div id="table"> <?php include("tablegetter.php"); ?> </div>
the html on main page works fine, drop down menu fills nicely distinct composer names in database. when option in menu selected thing echoed in "table" div "bonjour". it's not getting further if (isset($_get['composer']))
in tablegetter.php
page. i'm printing out querystring1
variable (the parameters) requested in getdata()
function , reports: ?composer=animuccia%2c%20paulo
works when loading page manually. won't work dynamically!
anybody know what's going on here?
you aren't setting querystring1
before sending ajax request. try rewrite of getdata()
.
function getdata() { var req = getxmlhttp(); if (req) { //function called when state changed req.onreadystatechange = function() { //when state completed i.e 4 if (req.readystate == 4) { // if http status "ok" if (req.status == 200) { var ajaxsearchresults1 = document.getelementbyid("table"); ajaxsearchresults1.innerhtml = req.responsetext; } else { alert("there problem while using xmlhttp:\n" + req.statustext); } } } var new1 = document.getelementbyid('composer').value; var querystring1 = "?composer=" + encodeuricomponent(new1); req.open("get", "tablegetter.php" + querystring1, true); req.send(); } }
Comments
Post a Comment