tags:

views:

52

answers:

3

Initially, I used a .js file that contains AJAX functions 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.

+1  A: 

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); }

);

Pekka
I used it, but not followed how to handle function(data). Previously I was not dealing with any callbacks. How to use this "data" to fill the DropDown.
RPK
Just replace the `#myDivID' part by the ID of your container, that should be all.
Pekka
I did, but it is not filling the list. However, it is taking the default text that I gave for the DropDown in the PHP code.
RPK
can you analyze the returned HTML code in firebug or using an alert() and post it in your question?
Pekka
I analyzed the "alert(data)" and found that actually the DropDown name that I am passing are not actually reaching the .php file.
RPK
+1  A: 

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.

Parrots
I do have a Div.
RPK
@RPK: as it stands, by using `$.get`, you're not assigning the select box to go anywhere. You need to use the technique that Parrots described in order to physically *put* it somewhere. You're effectively loading the code, but not doing anything with it.
mattbasta
I modified accordingly, but still it is not filling. It just changes the DropDown with text "Select from the list" in it. This text, as you can see in the original post code, is in the .php file. But it is not retrieving the list.
RPK
+1  A: 

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&param2=inMemberOf&param3=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.

sberry2A
Thanks but now it worked. Actually I forgot to replace "params" with my parameter name.
RPK