2

我创建了一个编辑表单,其中值将通过 ajax 在我的 codeigniter 控制器中发送。要更新的表单中的值是使用序列化函数传递var curr_val = $("#edit_currency").serialize();的,但要更新的值的 id 不包含在序列化方法中,只是通过传递给我的 javascript 函数var curr_id = $("#curr_id").val(); 问题是我无法成功传递这 2 个变量在我的控制器功能中接收到的ajax。没有更新发生。我怎样才能完成这项工作?非常感谢。这是我的代码

看法:

<?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));
 }
4

1 回答 1

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 也连接为查询字符串。

于 2013-08-18T23:37:23.280 回答