最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论