Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to implement my onchange method in my select option with xmlhttprequest (in my case, jquery is forbidden to used).

Here is my select option code :

<tr><td>
<form name="group" id="form1">
  <select name="group" id="group" onchange="member();">
    <option value="" disabled selected>Choose your group..</option>
      <?php foreach ($userGroups['data'] as $groups) {
         echo "<option value=\"".$groups['id']."\">".$groups['name']."</option>";
      }?>
  </select>
</form>
</td></tr>

<tr><td id="fetchmember">
   <!-- I HAVE NO IDEA WHAT CODE THAT I SHOULD WRITE HERE -->
</td></tr>

PHP code to process the input (fetchmember.php)

<?php
  include 'facebookauth.php';

    $groupId = $_GET['group'];
    $groupmember = $facebook->api('/'.$groupId.'/members');
    $membergroup = $groupmember['data'];

    foreach ($membergroup as $membergroups) {
      echo "<li>".$membergroups['name']."</li>";        
    }

?>

Array result from one of the selected group ($membergroup) :

Array ( [0] => 
   Array ( [name] => Oryza NurFa 
           [administrator] => 
           [id] => 1645819602 ) 
        [1] => 
   Array ( [name] => Muhammad Lathif Pambudi 
           [administrator] => 
           [id] => 100000251643877 ) 
        [2] => 
   Array ( [name] => Novantio Bangun 
           [administrator] => 
           [id] => 1152078197 ) 
        [3] => 
   Array ( [name] => Oliver Jordan 
           [administrator] => 
           [id] => 1065251285 ) 
        [4] => 
   Array ( [name] => Fauzan Sandy 
           [administrator] => 
           [id] => 1434833113 ) 
        [5] => 
   Array ( [name] => Arfan Fudyartanto D. N 
           [administrator] => 1 
           [id] => 1708602453 ) )

Javascript

<script type="text/javascript">
  function member(str)
  {
    if (str==0)
    { 
      document.getElementById("fetchmember").innerHTML="";
      return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        document.getElementById("fetchmember").innerHTML=xmlhttp.responseText;
      }
    }
  xmlhttp.open("GET","fetchmember.php?group="+str,true);
  xmlhttp.send();
  }
</script>

Data that I get is an array. Every time the select option is changed (onchange), I want the script loads the fetchmember.php and print the $membergroups['name'] (the member's name from the selected facebook group) automatically. Is it possible to directly retrieve an array data? I think the array must be converted to XML/JSON. I dont know how to do that. I know that there are still many deficiencies in my code, whether in php,html or js.

Please help. Any advice would be greatly appreciated. Thank you

share|improve this question
 
why no jQuery or any Ajax library? if you web code should work in ie5, you should use back-end browser detect tools and control the view for modern browsers and legacy browsers.use jquery in modern browser view and no any front end script in legacy browser view. legacy browser may not following the javascript standard and json –  caoglish Oct 17 '13 at 4:38
 
I knew it would be very easy with jquery. But i said before that in my case jquery is forbidden to used –  King Goeks Oct 17 '13 at 4:42
 
<td><ul id="fetchmember"></ul></td>. try this way –  Bhavik Shah Oct 17 '13 at 5:00
add comment

2 Answers

Try this:

I think you should convert array you return into JSON format.
And then after getting this in ajax response.
Finally convert it into regular format using javascript function and display into the div.

-

Thanks

share|improve this answer
 
Very short and sweet solution, Anand +1 for that. –  Chandresh Oct 18 '13 at 6:37
 
Thank You Sir .....! –  Anand Solanki Oct 18 '13 at 8:17
add comment
up vote 0 down vote accepted

Just do it with jquery.

jQuery("#fetchmember").empty();
jQuery.each(data, function(i, item) {
    jQuery("#fetchmember").append("someHTML");
});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.