1

In the following AJAX function, I'm trying to implement push array. There is problem with that array: I am not getting that array value in post in alert I am getting that value in array but when passing that value it's undefined.

  <script type="text/javascript">

  function getmember()
  {
    var myarray = new Array();
    myarray.push($(".group_id").val());
    alert(myarray);
     $.ajax({
      type: "GET",
      url: "getmember.php", 
      data: myarray,
      success: function(data) {
        $(".the-return").html(
          "Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: " + data["favorite_restaurant"] + "<br />Gender: " + data["gender"] + "<br />JSON: " + data["json"]
        );

        alert("bb");
      }
    });
    return false;
  } 
  </script>

<select name="group_id" class="group_id" onChange="getmember();" multiple>

             <?php
               $selectGroup = "SELECT group_id,group_name FROM `group`";
               $groupRes           = mysql_query($selectGroup);
               while($row1 =  mysql_fetch_array($groupRes))
               {
                 echo '<td><option value="'.$row1['group_id'].'">'.$row1['group_name'].'</option></td>';
               }
             ?>
          </select>
7
  • Do you mean the alert(myArray) shows nothing? can you show the HTML for .group_id? Commented May 25, 2015 at 11:02
  • @AmmarCSE html code updated. Commented May 25, 2015 at 11:05
  • where is group_id class name in HTML you posted? Commented May 25, 2015 at 11:06
  • Some punctuation in your prose would really help to make it understandable. I've added some where I could figure it out, but there's more missing after I am not getting where I'm not sure what you meant. Commented May 25, 2015 at 11:14
  • @das-g where is problem in my code Commented May 25, 2015 at 11:17

1 Answer 1

1

push value with key and define variable myarray as global

   var myarray; 
   function getmember()
   {
     myarray = [];
     myarray.push({'group_id':$(".group_id").val()});
    // your code.
   }

as discussed to post data like below URL

http://localhost/ashutosh/getmember.php?group_id=1,2,3,4

UPDATED CODE:

 var myarray;
 function getmember()
 {
   myarray = [];
   myarray.push($(".group_id").val());
   // your code.

    url: "getmember.php", 
    data:'group_id='+myarray.join(),
 }
Sign up to request clarification or add additional context in comments.

9 Comments

now i want to used this id in select query what should i do for it me implode that in my php file or that is possible with something else
chat option is block at my work place see this example tutorialspoint.com/mysql/mysql-in-clause.htm
SELECT * FROM your table WHERE group_id IN ( 1,2,3 );
suppose if i am selecting one value after that i am selecting another value than it pass first value two time..
my select query is like this $group_id = $myarray[]; $selectGroup = "SELECT contact_id FROM addressgroup WHERE group_id IN($group_id)";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.