Hi i need some help,how to do this in jquery.in the <select> how can i load the data to the select element that was queried in the database...
for example
Code:<select> <opption></option> </select>
| SitePoint Sponsor |




Hi i need some help,how to do this in jquery.in the <select> how can i load the data to the select element that was queried in the database...
for example
Code:<select> <opption></option> </select>

Hi there,
Dynamically creating a <select> element and adding a few <options> to it isn't too hard.
But it would help to know what form is the data in.
How are you fetching it from the database?
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog




Hi thank you for the reply,Okay i just tried to alert first so that i can see what is the data returned but it fails to alert
this how i query my string to other page
toselectdropdown.phpCode:$(function(){ $.ajax ({ type: "POST", data: "id="+myid, url: "members.php", success: function(r) { var id = r.empid; $('#emp').val(r.empname); mydropdown(id); } }); }); function mydropdown(id){ $.getJSON('toselectdropdown.php','empid = id',getEmpId); function getEmpId(e){ alert (e.emp); } }
but it will not alert.I hope you can help me on this.Code:<?php if(isset($_POST['empid'])){ $empid = $_POST['empid']; $sql = mysql_query("select * from myemployee where emp_id = $empid"); if(!$sql){ die("Failed".mysql_error()); } while($row = mysql_fetch_array($sql,MYSQL_BOTH)){ $empaddr= $row['emp_addr']; $emp_data = array('emp'=>$empaddr); echo json_encode($emp_data); } } } ?>
Thank you in advance.

Hi,
Try changing this:
Code JavaScript:$.getJSON('toselectdropdown.php','empid = id',getEmpId); function getEmpId(e){ alert (e.emp); }
Into this:
Code JavaScript:$.getJSON('toselectdropdown.php','empid = id', function(e){ alert(e.emp); });
Does that help?
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog





Hi there,
The syntax of the first part looks fine to me.
What we need to check now, is what is being returned from the initial ajax call.
Can you change this:
Code JavaScript:success: function(r) { var id = r.empid; $('#emp').val(r.empname); mydropdown(id); }
to this:
Code JavaScript:success: function(r) { console.log(r); }
and post the results.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog





If you test this in Chrome, press F12, click on the console tab and reload the page.
Then you will see the output of console.log()
Which browser are you using out of interest?
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog













OK, and what happens if you change:
Code JavaScript:success: function(r) { var id = r.empid; $('#emp').val(r.empname); mydropdown(id); }
to
Code JavaScript:success: function(r) { var id = r.empid; console.log(id); }
Presumably the output will be "undefined".
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog




i get 0001,
but if i use this
there is no output.function mydropdown(id){
$.getJSON('toselectdropdown.php','empid = id',function(e){
console.log(e);
});
}
I think the reason why i cannot display is because empid = id,instead of 0001,the display is "id".
can you please see the getJSON function if that is correct especiall y in the empid= id,
Thank you in advance.

Hi there,
You always come on line the minute I step away from the PC
What happens if you try this:
Code JavaScript:function mydropdown(id){ $.getJSON('toselectdropdown.php', {empid : id}, function(e){ console.log(e); }); }
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog




Hi,
I get syntax error: missing: after the property id
I also tried this by surrounding the curly braces into single quote,it runs but still i get null because the id cannot get 0001 value.it will display always like this {id= cls}{empid : id}
Code:function mydropdown(id){ $.getJSON('toselectdropdown.php', '{empid : id}', function(e){ console.log(e); });

Typo. That should be empid: (no space)
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog





Hi, I'll have to have a look at this later, as I'm away from the PC right now.
I'm sure we'll be able to get to the bottom of it.
In the meantime, could you please post a typical response from toselectdropdown.php, i.e. What you would hope to get back from a sucessful call.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog





Hi there,
So I'm just having a play around with this.
Let's summarize:
Code JavaScript:$.ajax ({ type: "POST", data: "id="+myid, url: "members.php", success: function(r) { var id = r.empid; $('#emp').val(r.empname); mydropdown(id); } });
This works as expected.
r contains:
Code:Object { empid="0001", empname="Christina"}Code JavaScript:function mydropdown(id){ $.getJSON('toselectdropdown.php','empid = id',getEmpId); function getEmpId(e){ alert (e.emp); } }
This is where the problems start.
What is the reason you are using getJSON?
Does it have any advantage over $.ajax?
I can make your script work when toselectdropdown.php echos a JSON string and I pass the id as before ({empid: id}).
i.e. if I place this in the code:
it works, but if I change it to this:PHP Code:echo json_encode(array("empid"=>"0001", "empname"=>"Christina"));
it dies silently.PHP Code:$empid = $_POST['id'];
echo json_encode(array("empid"=>$empid,"empname"=>"Christina"));
Can you therefore do this:
Code JavaScript:function mydropdown(id){ $.ajax ({ type: "POST", url: "toselectdropdown.php", data: { "id": id }, dataType: "json", success:function(r) { console.log(r.id); } }); }
On my setup, this worked fine, but as there are DB queries etc involved, it is quite hard to replicate everything.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog





Hi jemz,
The rest is simple.
Presuming that a successful call to toselectdropdown.php returns the following JSON object:
You can turn this into a <select> menu with the following code:Code:Object {1: "Option one", 2: "Option two", 3: "Option three"}
Code JavaScript:function mydropdown(id){ $.ajax ({ type: "POST", url: "toselectdropdown.php", data: { "id": id }, dataType: "json", success:function(data) { console.log(data); var items = []; $.each(data, function(key, val) { items.push('<option value="' + key + '">' + val + '</option>'); }); $('<select/>', { 'class': 'my-new-select', html: items.join('') }).appendTo('body'); } }); }
Hope that helps you.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog









Ok, well it sounds like we're almost there.
What we need to know is what toselectdropdown.php is returning when you have select * from employee in your code.
Can you therefore change this:
Code JavaScript:success:function(data) { var items = []; $.each(data, function(key, val) { items.push('<option value="' + key + '">' + val + '</option>'); }); $('<select/>', { 'class': 'my-new-select', html: items.join('') }).appendTo('body'); }
back to this:
Code JavaScript:success:function(data) { console.log(data); }
and post the output.
Cheers.
BTW, Array.join() is a method which joins the elements of an array into a string, and returns the string.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog
Bookmarks