Hi jemz,
Did you use the PHP code I provided?
It sounds like you have managed to make the PHP file return something sensible and have constructed a <select> menu from this data.
Is that the case?
Is ‘id’ in scope?
If so, you can mark the current employee as selected thus:
$("#my-new-select option").each(function(){
if($(this).val()==id){
$(this).attr("selected","selected");
}
});
Let me know if that works.
Happy Christmas!
jemz
December 27, 2012, 4:50am
42
James_Hibbard:
Hi jemz,
Did you use the PHP code I provided?
It sounds like you have managed to make the PHP file return something sensible and have constructed a <select> menu from this data.
Is that the case?
Is ‘id’ in scope?
If so, you can mark the current employee as selected thus:
$("#my-new-select option").each(function(){
if($(this).val()==id){
$(this).attr("selected","selected");
}
});
Let me know if that works.
Happy Christmas!
Hi pullo,
success: function(data){
$.each(data, function(key, val) {
$('#my-new-select').append($('<option></option>').val(key).html(val));
if($(this).val()==id){
$(this).attr("selected","selected");
}
});
}
It will not select automatically…
Did you use the PHP code I provided?
yeah like this.
$result = mysql_query("SELECT * FROM employee");
while($row = mysql_fetch_array($result)){
$empdata[$row['id']] = $row['emp_name'];
}
mysql_close($con);
echo json_encode($empdata);
Merry Christmas!
jemz
December 27, 2012, 5:06am
43
James_Hibbard:
Hi jemz,
Did you use the PHP code I provided?
It sounds like you have managed to make the PHP file return something sensible and have constructed a <select> menu from this data.
Is that the case?
Is ‘id’ in scope?
If so, you can mark the current employee as selected thus:
$("#my-new-select option").each(function(){
if($(this).val()==id){
$(this).attr("selected","selected");
}
});
Let me know if that works.
Happy Christmas!
I got it now to select automatically
success: function(data){
$.each(data, function(key, val) {
if(key==id)
$('#my-new-select').append($('<option></option>').val(key).html(val).attr('selected','selected'));
else
$('#my-new-select').append($('<option></option>').val(key).html(val));
});
}
Yay
Just for my personal interest, could you please post your final code (similar to what you posted in the first post, but obviously the working version).
Thanks!
jemz
December 28, 2012, 2:58am
45
Hi pullo,
Here is now the working code,but please correct me if i am wrong.
$(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){
$.ajax({
type: 'post',
data: 'id='+id,
url: 'toselectdropdown.php',
success: function(data){
$.each(data, function(key, val) {
if(key==clscode)
$('#my-new-select').append($('<option></option>').val(key).html(val).attr('selected','selected'));
else
$('#my-new-select').append($('<option></option>').val(key).html(val));
});
}
});
}
}
toselectdropdown.php
if(isset($_POST['id'])){
$con = mysql_query("select * from employee");
if(!$con)
die("Unable to load".mysql_error());
$data=array();
while($row = mysql_fetch_array($con)){
$empdata[$row['emp_id']]=$row['emp_address'];
}
}
header('Content-type: application/json');
echo json_encode($empdata);
}
Thanx jemz,
It was just with all of the back and forth, I’d lost sight of what the working solution ended up being.
The code looks ok.
There are a few small optimizations you could make, for example creating your <options> as a document fragment, then appending them to the <select>, but if this code works for you, then I would leave it at that.
jemz
December 29, 2012, 1:08pm
47
James_Hibbard:
Thanx jemz,
It was just with all of the back and forth, I’d lost sight of what the working solution ended up being.
The code looks ok.
There are a few small optimizations you could make, for example creating your <options> as a document fragment, then appending them to the <select>, but if this code works for you, then I would leave it at that.
your welcome :)…yup this code works for me.
jemz
January 3, 2013, 3:21pm
48
James_Hibbard:
Thanx jemz,
It was just with all of the back and forth, I’d lost sight of what the working solution ended up being.
The code looks ok.
There are a few small optimizations you could make, for example creating your <options> as a document fragment, then appending them to the <select>, but if this code works for you, then I would leave it at that.
Hi pullo,I’m back…I just want to ask in this php code that you gave
$result = mysql_query(“SELECT * FROM employee”);
while($row = mysql_fetch_array($result)){
$empdata[$row[‘id’]] = $row[‘emp_name’];
}
mysql_close($con);
echo json_encode($empdata);
how can i make if i want 3 columns in my employee table to be populate,the 2 columns will be on the dropdown,the one is for the textfield,i tried something like this but it doesn’t work.
$result = mysql_query(“SELECT * FROM employee”);
while($row = mysql_fetch_array($result)){
$empdata[$row[‘id’]] = $row[‘emp_name’]=$row[‘emp_age’];
}
mysql_close($con);
echo json_encode($empdata);
Thank you in advance.
Hi jemz,
One thing you could do is to create an array of arrays in your PHP script.
For example:
[ [id=>‘’, name=>‘’, age=>‘’], [id=>‘’, name=>‘’, age=>‘’], [id=>‘’, name=>‘’, age=>‘’] ]
You could then return it with json_encode
and access all of the values you need on a per employee basis.
<?php
$arr = Array();
$con = mysql_connect("localhost","...","...");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM employee");
while($row = mysql_fetch_array($result)){
$temp['id'] = $row['id'];
$temp['name'] = $row['emp_name'];
$temp['age'] = $row['emp_age'];
array_push($arr, $temp);
}
mysql_close($con);
echo json_encode($arr);
?>
Then in your JavaScript:
function mydropdown(id){
$.ajax
({
type: "POST",
url: "toselectdropdown.php",
data: { "id": id },
dataType: "json",
success:function(data)
{
for (var i=0;i<data.length;i++){
console.log(data[i]['id']);
console.log(data[i]['name']);
console.log(data[i]['age'] + '\
');
}
}
});
}
Hope this helps.
jemz
January 4, 2013, 2:46pm
50
James_Hibbard:
Hi jemz,
One thing you could do is to create an array of arrays in your PHP script.
For example:
[ [id=>‘’, name=>‘’, age=>‘’], [id=>‘’, name=>‘’, age=>‘’], [id=>‘’, name=>‘’, age=>‘’] ]
You could then return it with json_encode
and access all of the values you need on a per employee basis.
<?php
$arr = Array();
$con = mysql_connect("localhost","...","...");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM employee");
while($row = mysql_fetch_array($result)){
$temp['id'] = $row['id'];
$temp['name'] = $row['emp_name'];
$temp['age'] = $row['emp_age'];
array_push($arr, $temp);
}
mysql_close($con);
echo json_encode($arr);
?>
Then in your JavaScript:
function mydropdown(id){
$.ajax
({
type: "POST",
url: "toselectdropdown.php",
data: { "id": id },
dataType: "json",
success:function(data)
{
for (var i=0;i<data.length;i++){
console.log(data[i]['id']);
console.log(data[i]['name']);
console.log(data[i]['age'] + '\
');
}
}
});
}
Hope this helps.
Hi pullo, thank you for the reply…Okay i will write back to you if i have some doubt.