javascript - How to pass multiple variables through ajax to codeigniter controller? One variable is via serialize - Stack Overfl

I have created an edit form in which values are to be send in my codeigniter controller via ajax. The v

I have created an edit form in which values are to be send in my codeigniter controller via ajax. The values in the form that are to be updated is passed using serialized function var curr_val = $("#edit_currency").serialize(); but the id of the values to be updated is not included in the serialize method and is just passed to my javascript function via var curr_id = $("#curr_id").val(); The problem is I cannot successfully passed this 2 variable in ajax to be received in my controller function. There is no updating happens. How can I get this done? Thanks a lot. Here are my codes

View:

<?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>

Controller:

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

I have created an edit form in which values are to be send in my codeigniter controller via ajax. The values in the form that are to be updated is passed using serialized function var curr_val = $("#edit_currency").serialize(); but the id of the values to be updated is not included in the serialize method and is just passed to my javascript function via var curr_id = $("#curr_id").val(); The problem is I cannot successfully passed this 2 variable in ajax to be received in my controller function. There is no updating happens. How can I get this done? Thanks a lot. Here are my codes

View:

<?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>

Controller:

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));
 }
Share Improve this question asked Aug 18, 2013 at 23:17 EliEli 1,2765 gold badges32 silver badges63 bronze badges 1
  • What is the response you get from server, when you see it in the Developer tools? Cannot pass means your CI function is not executed at all or executed but you miss the values? – Nish Commented Aug 19, 2013 at 6:11
Add a ment  | 

1 Answer 1

Reset to default 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);
          }


        }
    });

You converted the curr values to query string so you just need to concat the curr id as query string as well.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745654693a4638473.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信