Send same name multiple checkbox values via ajax

I have multiple checkbox input elements.

<input type="checkbox" name="userpages[]" id="1" value="1"/>
<input type="checkbox" name="userpages[]" id="2" value="2"/>
<input type="checkbox" name="userpages[]" id="3" value="3"/>
<input type="checkbox" name="userpages[]" id="4" value="4"/>

I want to pass the value of checked element to the php script via Ajax. I tried doing it this way -

var pages = $('input[name="userpages[]"]:checked');
    $.ajax({
    type: 'POST',
    url: 'post.php',
    data: {pages: pages},
    dataType: 'json',
    success: function(data) {
        if(data.status == 1) {
            alert('Successfully posted on your Facebook pages !');
        } else if(data.status == 0) {
            alert('Error !! Please try again.');
        } else {
            alert('Unknown Error. Reloading this page now...');
            location.reload();
         }
            }
    });

and retrieved the value in php script -

  foreach($_POST['pages'] as $page_id) {
    echo $page_id;
  }

But this didn't worked for me. I also tried getting the value of variable pages, when alerted it popped up 'object Object'. Any help would be appreciable. :)

Answers


                var checked = []
        $("input[name='userpages[]']:checked").each(function ()
        {
            checked.push(parseInt($(this).val()));
        });

Need Your Help

Why must you call SelectObject before calling GetTextExtendPoint32

visual-c++

I understand that for GetTextExtendPoint32 to work correctly, it needs to know the correct font. However, I'm confused as to why SelectObject needs to be called. Example I wanted to calc the leng...

How to dynamically allocate big memory , like 10 G ,using new operator in c++ on 64-linux?

c++ linux memory

I need to dynamically allocate a larger float array for a special application using C++ new operator, like 10G. The code running on 64-ubuntu-14.04 Linux OS with 64G memory.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.