Query string used for AJAX not refreshing
I used following code to dynamically fill DropDown controls using AJAX. Each time a DropDown control is clicked, following statement transfers the query to an AJAX function which then forwards to a .php file to retrieve the database results.
The problem is that the same code works absolutely fine when used on the local machine even if the MySQL connection string is using the same remote server's IP address as the host. But when I upload the files to the remote server, all DropDowns fill the same results. For example, if there are three DropDowns viz: District, Constituency, City and if I click say, City, than all the other DropDowns also shows cities in their list.
I guess the query string that I am using in the following AJAX function is not refreshing.
[HTML DropDown Code Sample]
<div id="divDistrict" name="divDistrict">
<select class="element select medium" id="inDistrict" name="inDistrict" onclick="MakeRequest('divDistrict', 'inDistrict', 'SELECT * FROM districtmaster');">
<option value="Select" selected="Select">Select</option>
</select>
</div>
[AJAX Function]
function MakeRequest(DivName, DropDownName, SqlQuery)
{
var xmlHttp = getXMLHttp();
var strUrl = "../Lib/filldropdown.php?DivName=" + DivName + "&DropDownControlName=" + DropDownName + "&SqlQuery=" + SqlQuery;
try
{
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText, DivName);
}
}
xmlHttp.open("GET", strUrl, true);
xmlHttp.send(null);
}
catch(err)
{
alert(err);
}
}
[The PHP file code]
<?php
require("dbconnection.php");
require("dbaccess.php");
$dropdownControlName = $_GET['DropDownControlName'];
$query = $开发者_JS百科_GET['SqlQuery'];
dbconnection::OpenConnection();
$result = dbaccess::GetRows($query);
?>
<select name=" <?php $dropdownControlName ?> ">
<option>Select from the list</option>
<?php while($row=mysql_fetch_array($result))
{ ?>
<option value="<?= $row[0] ?>"><?= $row[1] ?></option>
<?php } ?>
</select>
Please ignore the DBAccess & DBConnection references. Those files contains simple mysql_query & mysql_connect functions. Also, I am aware of the risk of passing the query in the query string. I will fix it once the DropDown fill works correctly.
Run Fiddler or Firebug and check exactly what is being sent to the server.
Typical causes of such problems:
- You uploaded the script the wrong way.
- The same item list is loaded into all the select elements accidentally. (due to typos or so)
- Your server has some kind of "caching" installed.
- There is a proxy between you and the server which does not consider the change in the GET parameters.
- The browser caches the result somehow, for example due to cache headers added by the Web server automatically. (Check the response headers of your XHR requests in Firebug.)
Ok, solved the problem. I figured out the issue. The PHP code with the line:
<select name=" <?php $dropdownControlName ?> ">
needs to be corrected with this
<select name="<?php echo $dropdownControlName; ?>">
It is working now. Thanks to all.
精彩评论