Option

:nanaman:

Hi, pullo, Jackpot :nanaman: , it will display now the value,…and now how can i display this in the dropdownlist ?

Hi jemz,

The rest is simple.

Presuming that a successful call to toselectdropdown.php returns the following JSON object:

Object {1: "Option one", 2: "Option two", 3: "Option three"} 

You can turn this into a <select> menu with the following code:

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.

Hi, pullo, can i ask what is this piece of code means

items.join(‘’)

Hi, I tried it but it will not work, but when i only select with a certain id it will display…

Example:
This will not work

Select * from employee

This will work

select empid from employee where empid = $id

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:

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:

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.

Hi pullo,

Thank you for the reply…do i need to do this?

$(‘<select/>’, {
‘class’: ‘my-new-select’,
html: items.join(‘’)
}).appendTo(‘body’);

but i have already <select> element in my html…
my html code goes something like this


    
 <label>choose</label>

<select id="optselect">
  <option></option>
</select>


Hi,

Thanks for that.
If you have a select option with an id, things are even easire.
But first, I need to know what toselectdropdown.php is returning when you have select * from employee in your code.

Re-read my last post, alter the code accordingly and post the output here.

Hi pullo, this is the result of log

(an empty string)

hi pullo, i tried to use this


error:(jqXHR,textStatus){
  alert("error" + textStatus);
},
success: function(){
  some code....
});


when my code runs it will go to the error and it alert to “pareserror”.

maybe in sql statement the way i select *.?

okay i fixed now the parse error: it’s in my die statement

now i have new problem when it display to the dropdownlist my text value is in vertical align like this
sample

w
e
l
c
o
m
e
{
"

etc…

here is how i select.
please correct me if i am wrong.


if(isset($_POST['id'])){




          $con = mysql_query("select * from employee");

          if(!$con)
              die("problem".mysql_error());


      while($row = mysql_fetch_array($con,MYSQL_BOTH)){

               $empname= $row['emp_name'];
               $emaddr= $row['emp_addr'];

            $empdata = array('empname=>$empname,'empaddr'=>$emaddr);


          echo json_encode($empdata);
      }




    }

Hi jemz,
Back to the original question, I am afraid.
What data is being returned to the success callback?
Can you log it to the console, as per Post#25
Thanx

BTW, just looking at your PHP code.
I would have thought the correct way to do things, was to declare the $empdata array before your while loop, then append values to it within the while loop, then after the while loop and ouside of the if statement, echo the array using json_encode.

Hi,pullo

here is the log

{“empname”:“jack”,“empaddr”:“Australia”}

Is this the output when you have “select * from employee” in your PHP code?
How many records are in the data base?

Try changing the PHP according to my suggestions in post 32

Hi, you mean like this


if(isset($_POST['id'])){




          $con = mysql_query("select * from employee");

          if(!$con)
              die("problem".mysql_error());

      $empdata = array();
      while($row = mysql_fetch_array($con,MYSQL_BOTH)){

               $empname= $row['emp_name'];
               $emaddr= $row['emp_addr'];

            $empdata = array('empname=>$empname,'empaddr'=>$emaddr);


         
      }




    }
 echo json_encode($empdata);

i have 20 records…

Hi jemz,

Let’s put it another way.
What do you want the function mydropdown(id) to do.

Should it fetch a list of employees from your database table and automatically select the current one?

Hi pullo, thank you for the reply…yes i want to fetch all list of employees and put it in the dropdown list and then select the current one

Ok, that should be no problem.
A select looks like this:

<select>
  <option value = "value">Some text</option>
  <option value = "value">Some text</option>
</select>

What should the value in the option be?
The employee’s id?

What should “Some text” be?
The employees name?

Edit:
I’ve just coded up something along these lines:

If you alter your “toselectdropdown.php” file like this:

<?php $con = mysql_connect("localhost","root","*****");
  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)){
    $empdata[$row['id']] = $row['emp_name'];
  }

  mysql_close($con);
  echo json_encode($empdata);
?>

Then use the JavaScript code from post 22 to create the drop down, you should see something approaching what you are trying to achieve.
It’s not the finished product, but let me know if this works for you.

Hi pullo, but what if i have made already in my html file the <select></select>
do i need to do this

$(‘<select/>’, {
‘class’: ‘my-new-select’,
html: items.join(‘’)
}).appendTo(‘body’);

Hi pullo, there is no output…but i tried some google i found this solution and it works for me.

success: function(data){

              $.each(data, function(key, val) {
                $('#my-new-select').append($('&lt;option&gt;&lt;/option&gt;').val(key).html(val));

          });

but the problem is i don’t know how to make selected option.

when all the key and value will be loaded in the dropdown,i want that it will select automatic which id is being search.
example:

data: ‘id=’+0005;

i want that employee having 0005 will be selected automatically in the dropdownlist…how can i do this?..

Thank you in advance.