Mif.ComicVN
Mif.ComicVN

Reputation: 87

How to send an array to php by jquery without a form with multi input same name

I want send an array to my codeigniter app for deleted. I know i can do a form with multi input same name . But can i ask how can do that without a form ? Can i post with ajax ?

//HTML Code

<li class="bigbox empty" id="bigbox_52"><a title="" href="#" class="abigbox selectbox" rel="52" style="opacity: 0.5;"></li>
<li class="bigbox empty" id="bigbox_37"><a title="" href="#" class="abigbox selectbox" rel="37" style="opacity: 0.5;"></li>
<li class="bigbox empty" id="bigbox_36"><a title="" href="#" class="abigbox selectbox" rel="36" style="opacity: 0.5;"></li>
<li class="bigbox empty" id="bigbox_38"><a title="" href="#" class="abigbox selectbox" rel="38" style="opacity: 0.5;"></li>
<li class="bigbox empty" id="bigbox_39"><a title="" href="#" class="abigbox selectbox" rel="39" style="opacity: 0.5;"></li>

<a class="delall" href="">Delete All</a>

//Jquery Code

$(".delall").click(function(){
    var values = $(".selectbox").map(function(){
    return $(this).attr('rel');
    }).get();

        return false;
});

Please help me to handle this array , soory because i m new with Jquery. Many thanks.

Upvotes: 0

Views: 260

Answers (3)

osahyoun
osahyoun

Reputation: 5231

How about something like this:

function postArray(){
  var mapTo = function(){
    return $(this).attr('rel');
  },

  values = $(".selectbox").map(mapTo).get(),
  url = "http://my-server.com/resource.php";

  $.ajax({
    type: 'POST',
    url: url,
    data: {'values[]':values},
    success: function(response){
      console.log(response);
    }
  });
}

$(".delall").click(postArray);

jQuery's documentation is thorough and has some good examples. Docs on $.ajax can be found here: http://api.jquery.com/jQuery.ajax/

Upvotes: 1

Jordan Arsenault
Jordan Arsenault

Reputation: 7388

It's not quite clear what you're trying to accomplish with the list elements, but yes, first construct an object in Javascript and call the $.post() function. Why not use checkboxes? What's all this bigbox stuff?

$(document).ready(function(){

    //your previous code here to get the proper values.

    var url = "yourapp.com/yourcontroller/yourfunction";
    $.post(url, values, function(response){
      //when the server responds, log the response.  
      console.log(response);
    })
});

Upvotes: 0

Sapan Diwakar
Sapan Diwakar

Reputation: 10946

You can use jQuery post (or any other AJAX call) to send an array:

$.post("test.php", { 'choices[]': ["Jon", "Susan"] });

For details, have a look here.

Upvotes: 2

Related Questions