Eli
Eli

Reputation: 1276

How to pass multiple variables through ajax to codeigniter controller? One variable is via serialize

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

Upvotes: 2

Views: 10000

Answers (1)

Roseann Solano
Roseann Solano

Reputation: 768

 $.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.

Upvotes: 2

Related Questions