SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 50 of 50

Thread: option

  1. #26
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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.
    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
    Code:
        
     <label>choose</label>
    
    <select id="optselect">
      <option></option>
    </select>

  2. #27
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by jemz View Post
    but i have already <select> element in my html...
    my html code goes something like this
    Code:
    <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.

  3. #28
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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)

  4. #29
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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, i tried to use this
    Code:
    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 *.?

  5. #30
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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.
    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.
    Code:
    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);
          }
    
    
    
    
        }

  6. #31
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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

  7. #32
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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.

  8. #33
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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

    Hi,pullo

    here is the log

    {"empname":"jack","empaddr":"Australia"}

  9. #34
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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

  10. #35
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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

    Code:
    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..

  11. #36
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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?

  12. #37
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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

  13. #38
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by jemz View Post
    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:

    HTML Code:
    <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 Code:
    <?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.

  14. #39
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Ok, that should be no problem.
    A select looks like this:

    HTML Code:
    <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 Code:
    <?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');

  15. #40
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Ok, that should be no problem.
    A select looks like this:

    HTML Code:
    <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 Code:
    <?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, 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($('<option></option>').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.

  16. #41
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi jemz,

    Quote Originally Posted by jemz View Post
    Hi pullo, there is no output...but i tried some google i found this solution and it works for me.
    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:

    Code JavaScript:
    $("#my-new-select option").each(function(){
      if($(this).val()==id){
        $(this).attr("selected","selected");    
      }
    });

    Let me know if that works.

    Happy Christmas!

  17. #42
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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:

    Code JavaScript:
    $("#my-new-select option").each(function(){
      if($(this).val()==id){
        $(this).attr("selected","selected");    
      }
    });

    Let me know if that works.

    Happy Christmas!

    Hi pullo,

    Code:
     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.

    Code:
     $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!

  18. #43
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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:

    Code JavaScript:
    $("#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

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

  19. #44
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by jemz View Post
    I got it now to select automatically
    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!

  20. #45
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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!
    Hi pullo,

    Here is now the working code,but please correct me if i am wrong.

    Code:
     $(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
    Code:
    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);
        }

  21. #46
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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.

  22. #47
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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.

  23. #48
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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.

  24. #49
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,075
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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 Code:
    <?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:

    Code 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'] + '\n');
          }
        }
      });
    }

    Hope this helps.

  25. #50
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,159
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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 Code:
    <?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:

    Code 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'] + '\n');
          }
        }
      });
    }

    Hope this helps.
    Hi pullo, thank you for the reply...Okay i will write back to you if i have some doubt.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •