Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <select id="selectWarrior_1" name="warrior_1" onclick="return selectWarriors()">
  2. <option selected="selected">----Select----</option>
  3. </select>
  4.  
  5. function selectWarriors() {
  6. $.ajax({
  7. url: "display_warriors.php",
  8. datatype:"json",
  9. success: function(data) {
  10. var toAppend = '';
  11. if(typeof data === "object"){
  12. for(var i=0;i<data.length;i++){
  13. var warrior = data[i];
  14. toAppend += '<option>'+data[i]['warrior_name']+'</option>';
  15. }
  16. $("#selectWarrior_1 select").append(toAppend);
  17. }
  18. }
  19. });
  20. return false;
  21. }
  22.  
  23. <?php
  24. header("Content-Type: application/json");
  25.  
  26. include 'functions/class.php';
  27.  
  28. $db = new DB();
  29.  
  30. $result = $db->getWarriors();
  31.  
  32. $warriors = array();
  33.  
  34. foreach($result as $names){
  35. $warriors[] = $names;
  36. }
  37. echo json_encode($warriors);
  38. ?>
  39.  
  40. $(function(){
  41. $('#selectWarrior_1').bind('click',function(){
  42. if ( $(this).data('warriors-loaded') ) { return; }
  43. var self = $(this);
  44. $.ajax({
  45. url: "display_warriors.php",
  46. datatype:"json",
  47. success: function(data) {
  48. if (! typeof data == 'object') return false;
  49. $.each(data, function(i,e) {
  50. self.append( $('<option>' + e['warrior_name'] + '</option>') );
  51. }
  52. self.data('warriors-loaded',true);
  53. }
  54. });
  55. });
  56. });
  57.  
  58. function selectWarriors() {
  59. $.getJSON('display_warriors.php', function(data) {
  60. var toAppend = '';
  61. $.each(data, function(key, val) {
  62. toAppend += '<option>'+val+'</option>';
  63. });
  64. $("#selectWarrior_1").append(toAppend);
  65. });
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement