我需要一些幫助,通過使用ajax將複選框值傳遞到數組中並檢索控制器中的值。以下代碼不起作用。我收到錯誤:「爲foreach()提供的無效參數」。 Var_dump給出string(42) "Educator_Classes[]=1&Educator_Classes;[]=3
使用ajax將複選框值傳遞給數組php
感謝您提供任何幫助。
我的HTML表單輸入:
<input type="checkbox" id="Educator_Classes[]" name="Educator_Classes" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>
我的jQuery:
$("#Send_Invite").click(function() {
var form_data = {
Opportunity_Id: $('#Opportunity_Id').val(),
Educator_Id: $('#Educator_Id').val(),
Educator_Classes: $('#Educator_Classes:checked').serialize(),
ajax: '1'
};
$.ajax({
url: "<?php echo site_url('schedule/update_educator_class'); ?>",
type: 'POST',
data: form_data,
success: function(data) {
$('#response').html(data);
}
});
return false;
})
我的控制器:
function update_educator_class() {
$Educator_Id = $this->input->post('Educator_Id');
$Opportunity_Id = $this->input->post('Opportunity_Id');
$Educator_Classes = $this->input->post('Educator_Classes');
foreach($Educator_Classes as $Educator_Class):
$this->ion_auth_model->update_educator_class($Opportunity_Id, $Educator_Class, $Educator_Id);
endforeach;
}
選擇像這樣的educator_classes'Educator_Classes:$('[id =「Educator_Classes []」]')。serialize()'或'Educator_Classes:$('#Educator_Classes \\ [\\''')。serialize )' – Anton 2014-09-10 13:35:27
@安頓嗯,嘗試兩個,仍然得到相同的結果。感謝您的快速回復。 – 2014-09-10 13:39:09
啊,我注意到這是你想要的課程。你需要使用類指示符**''''**'$('.Educator_Classes:checked')' – Anton 2014-09-10 13:40:01