我需要一些帮助来获取提交按钮的值。下面的代码会触发我的控制器功能,但是我无法获得“邀请”按钮的值。 var_dump表示'bool(false)',Educator_Id的0被插入到我对数据库的最终查询中。
感谢您提供任何帮助!
我的提交按钮:
<?php foreach($educators as $educator): ?>
<button type="submit" id="Invite" name="Invite" value="<?php echo $educator->Educator_Id; ?>">Invite</button>
<?php endforeach; ?>
我的jQuery函数:
$("#Invite").click(function() {
var form_data = $('#validation-form').serialize();
$.ajax({
url: "<?php echo site_url('schedule/send_invite'); ?>",
type: 'POST',
data: form_data
});
return false;
})
我的控制器:
function send_invite() {
$email = $this->input->post('Educator_Email');
$Opportunity_Id = $this->input->post('Opportunity_Id');
$Educator_Id = $this->input->post('Invite');
$Class_Numbers = $this->input->post('Class_Numbers');
foreach($Class_Numbers as $Class_Number):
$this->ion_auth_model->update_class_educator($Opportunity_Id, $Class_Number, $Educator_Id);
endforeach;
}
我的模特:
function update_class_educator($Opportunity_Id, $Educator_Class, $Educator_Id) {
$Class = array(
'Educator_Id' => $Educator_Id
);
$this->db->where('Opportunity_Id', $Opportunity_Id);
$this->db->where('Class_Number', $Educator_Class);
$this->db->update('Classes', $Class);
}
答案 0 :(得分:0)
尝试将值传递给隐藏输入
JavaScript的:
$("#Invite").live("click", function(){
var this_value = $(this).val();
$("input[name='InviteValue']").val(this_value);
});
答案 1 :(得分:0)
而不是像这样使用按钮输入:
<?php
$count = 0;
foreach($educators as $educator): ?>
<input type="submit" id="Invite_$count" class="invite" name="Invite" data-value="<?php echo $educator->Educator_Id; ?>" value="Invite"/>
<?php
$count++;
endforeach; ?>
在jquery中你可以使用:
$(".invite").click(function() {
var submit_data = $(this).data('value');
var form_data = $('#validation-form').serialize();
$.ajax({
url: "<?php echo site_url('schedule/send_invite'); ?>",
type: 'POST',
data: form_data + "&submit_data=" + submit_data
});
return false;
})
在控制器中:
function send_invite() {
$email = $this->input->post('Educator_Email');
$Opportunity_Id = $this->input->post('Opportunity_Id');
$Educator_Id = $this->input->post('submit_data');
$Class_Numbers = $this->input->post('Class_Numbers');
foreach($Class_Numbers as $Class_Number):
$this->ion_auth_model->update_class_educator($Opportunity_Id, $Class_Number, $Educator_Id);
endforeach;
}
试试吧。这可能会解决您的问题。
答案 2 :(得分:0)
尝试使用PHP:
<?php
foreach($educators as $educator){
echo "<input type='checkbox' name='invite[]' value='".$educator->Educator_Id."'>".$educator."<br/>";
}
echo "<input type='submit' value="Invite"/>";
它将为每个教育工作者和单个提交按钮呈现一个复选框。然后,在你的JS中:
$("#invite").click(function() {
var form_data = $('#validation-form').serialize();
$.ajax({
url: "<?php echo site_url('schedule/send_invite'); ?>",
type: 'POST',
data: form_data,
success: function(){
// Do something here if succeded
}
});
return false;
});
这样它只会向服务器发送选中的复选框,因此您可以从那里处理控制器中的选定对象。
$Educator_Id = $this->input->post("invite[]");
而不是
$Educator_Id = $this->input->post('Invite');
我认为这应该有用......你的最终目标对我来说有点不清楚,但在这种情况下,你的逻辑是什么,这应该有助于澄清事情。
干杯!