PHP code called via JQuery stopped working
Initially, I used a .js file that contains AJAX f开发者_如何学Cunctions to call a .php file. The .php file contains code to dynamically populate a DropDown based on certain parameters passed via QueryString.
Everything works fine with the above method. But now I want to populate the DropDowns on page load and so want to use JQuery instead of my old method. I tried following code, but it is not filling the DropDown.
$.get("../Lib/filldropdown.php",
{ param1: "divMemberOf", param2: "inMemberOf", param3: "categorymaster" });
Below is the code of the .php file:
<?php
require("dbconnection.php");
require("dbaccess.php");
$dropdownControlName = $_GET['DropDownControlName'];
$query = ".....";
dbconnection::OpenConnection();
$result = dbaccess::GetRows($query);
?>
<select id="<?php echo $dropdownControlName; ?>" name="<?php echo $dropdownControlName; ?>">
<option>Select from the list</option>
<?php while($row=mysql_fetch_array($result))
{ ?>
<option value="<?php echo $row[0]; ?>"><?php echo $row[1]; ?></option>
<?php } ?>
</select>
Please note that the above JQuery code passes three parameters, but I have used only one in the PHP code above. I am going to use two parameters later. The above PHP code is perfectly working with my old AJAX method.
You are making an AJAX request using GET, but you are not adding a callback function to actually handle the returned HTML. It vanishes in thin air.
Check out the JQuery AJAX documentation on GET for a complete example.
Your call would have to look something like this:
$.get("../Lib/filldropdown.php",
{ param1: "divMemberOf", param2: "inMemberOf", param3: "categorymaster" },
function(data) { $('#myDivID').html(data); }
);
You either need to add a callback function, or the .load method might be more appropriate since you just want to inject the results from that PHP call into the page:
$("#someContainer").load("../Lib/filldropdown.php",
{ param1: "divMemberOf",
param2: "inMemberOf",
param3: "categorymaster" });
You just need some DIV or something to be a placeholder to inject the select box into.
You are not specifying the key correctly for your php to work as-is. In your javascript you are creating a request like this: param1=divMemberOf¶m2=inMemberOf¶m3=categorymaster
I would think you need one of those parameters to be named DropDownControlName since you are using it as the id for your newly created select input. You might also check param2 and param3 since you are likely using those in your query.
精彩评论