douqi1625 2017-03-03 07:47
浏览 48

$ _GET无法遍历数组

I have dataId from checkbox values and I'm sending it using a GET AJAX request:

dataId = 1, 2, 16, 15, 17, 3, 14, 5, 9, 11;
URI = "<?php echo site_url('Terpasang/Update_Kebutuhan_ke_Terpasang')?>";
$.ajax({
    url : URI,
    type: "GET",
    data: { "iddata": [dataId] },
    dataType: "JSON",
});

In controller I am passing this data with code:

$iddata = array($_GET['iddata']);
$Countdata = count($iddata);
for($i = 0; $i <= $Countdata; $i++)
{
    $data = array('id_perjal'=>$iddata[$i]); // this code can't generate id one by one like i wanna : 1, next 2, next 16 etc 
    $this->M_perjal_terpasang->save($data);
}
echo json_encode(array("status" => TRUE));
  • 写回答

4条回答 默认 最新

  • dpgui8229808 2017-03-03 07:55
    关注

    The issue is with how you build the array. You cannot set a variable equal to a comma list of values. If you check the code you'll see that dataId only has a single value:

    dataId = 1, 2, 16, 15, 17, 3, 14, 5, 9, 11;
    data = {
        "iddata": [dataId]
    };
    
    console.log(data);

    To fix this define the dataId as an array explicitly:

    var dataId = [1, 2, 16, 15, 17, 3, 14, 5, 9, 11]; // note the [] wrapping the values
    

    Then use it in the object you provide to data:

    data: { "iddata": dataId },
    
    </div>
    
    评论

报告相同问题?