最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send same name multiple checkbox values via ajax - Stack Overflow

programmeradmin4浏览0评论

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. :)

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. :)

Share Improve this question asked Mar 2, 2014 at 12:15 Nishant GhodkeNishant Ghodke 9231 gold badge12 silver badges21 bronze badges 1
  • 2 Debug using the console: console.log(/* variable */). Open the console in Chrome using F12. You get a lot more information about JS data, and you can also execute JS lines of code. – Hidde Commented Mar 2, 2014 at 12:19
Add a ment  | 

2 Answers 2

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

The array is correct. However, it is an array consisting of jQuery DOM elements, not the values of the inputs.

To get the data in the form of index => value pairs in the array, to send it by AJAX, use something like the following:

var data = []

$('input[name="userpages[]"').each (function (index, element) {
    data[$(element).attr('id')] = $(element).val();
});
console.log(data);

Send the gathered data using the jQUery AJAX function.

A JSFiddle: http://jsfiddle/LMbxC/1/

发布评论

评论列表(0)

  1. 暂无评论