我创建了一个编辑表单,其中的值将通过ajax在我的codeigniter控制器中发送。要更新的表单中的值使用序列化函数var curr_val = $("#edit_currency").serialize();
传递,但要更新的值的id不包含在serialize方法中,只是通过var curr_id = $("#curr_id").val();
传递给我的javascript函数
问题是我无法在ajax中成功传递这个2变量,以便在我的控制器函数中接收。没有更新发生。我怎么能这样做?非常感谢。这是我的代码
查看:
<?php
//echo form_open('/display/student_update');
foreach($curr_values as $row){
echo"<input type='hidden' name='curr_id' id='curr_id' value=".$row->id.">";
?>
<form method="post" action="" id="edit_currency">
<div class="row">
<div class="span4"><strong><?php echo $lbl_currency_name;?></strong> </div>
</div>
<div class="row">
<div class="span4"><strong><?php echo"<input type='text' name='currency[pretty_name]' id='pretty_name' value=".$row->pretty_name.">"; ?></strong> </div>
</div>
<div class="row">
<div class="span4"><strong><?php echo $lbl_currency_code; ?></strong> </div>
</div>
<div class="row">
<div class="span4"><strong><?php echo form_input($currency_code,$row->currency_code);?></strong> </div>
</div>
?>
<script type="text/javascript">
$(function(){
$("#currency_save").click(function(){
var curr_id = $("#curr_id").val();
var curr_val = $("#edit_currency").serialize();
alert(curr_id);
alert(curr_val);
$.ajax({
type: "POST",
url: "<?php echo base_url();?>currencies/update_currencies",
dataType:'json',
data: {'curr_values':curr_val,'curr_id':curr_id},
success: function(data)
{
if(data.notify=="Success"){
console.log(data.notify);
}
else{
console.log(data.notify);
}
}
});
$("html, body").animate({ scrollTop: 0 }, 600);
return false;
});
});
</script>
控制器:
function update_currencies(){
$curr_val=$this->input->post('curr_values');
$curr_id = $this->input->post('curr_id');
$query = $this->course_booking_model->update_currencies($curr_id,$curr_val);
if($query){
$notification = "Success";
}
else{
$notification = "Failed";
}
echo json_encode(array('notify'=>$notification));
}
答案 0 :(得分:2)
$.ajax({
type: "POST",
url: "<?php echo base_url();?>currencies/update_currencies",
data: curr_values + '&curr_id=' + curr_id,
success: function(data)
{
if(data.notify=="Success"){
console.log(data.notify);
}
else{
console.log(data.notify);
}
}
});
您将curr值转换为查询字符串,因此您只需将curr id连接为查询字符串。