jQuery - 发布多个复选框值的数组和php的标题

时间:2010-08-03 06:49:04

标签: php jquery foreach

我参考此页面:Post array of multiple checkbox values

<input type="checkbox" class="box"  value="blue" title="A" />
<input type="checkbox" class="box"  value="red" title="B" />
<input type="checkbox" class="box"  value="white" title="C"/>

$('#include').click(function(){

    var val = $('.box:checked').map(function(i,n) {
        return $(n).val();
    }).get(); //get converts it to an array         

    var title = $('.box:checked').map(function(i,n) {
        return $(n).attr('title');
    }).get(); //get converts it to an array     

    $.post(url, {'val[]': val,'title[]':title}, function(response) {
        alert('scuess');
    });
        return false;
})
---------------------------------------------
<?php
   foreach($_GET['val'] as $numA)
   {
        foreach($_GET['title'] as $numB)
       {
            $str="insert into user_photos (val,title) values ('$numA','$numB')";
            mysql_query($str,$link); 
       }
   }
?>

请告诉我该怎么做......

2 个答案:

答案 0 :(得分:0)

<input type="checkbox" class="box[A]" value="blue" />
<input type="checkbox" class="box[B]" value="red" />
<input type="checkbox" class="box[C]" value="white" />
<?
// load multi-dimensional array
$values = $_POST['box'];
foreach($values as $title) {
    foreach($title as $val) {
        // insert $title/$val into mySQL
    }
}
?>

答案 1 :(得分:0)

这样做......

的jQuery

$('#include').click(function(){

    var data = $('.box:checked').map(function(i,n) {
        return {'val':$(n).val(), 'title': $(n).attr('title')};
    }).get(); //get converts it to an array         

    $.post(url, {'data': data}, function(response) {
        alert('scuess');
    });
        return false;
});

PHP

<?php
   foreach($_GET['data'] as $data)
   {
       $str="insert into user_photos (val,title) values ('$data[val]','$data[title]')";
       mysql_query($str,$link); 
   }
?>