简体   繁体   中英

jquery ajax call in codeigniter

i have two function one is for delete and another for update . My delete function is working correctly but when i have written update function that is not working . Also update not working. Here is the view

<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>

<script>
    $(document).ready(function()
    {
        $('table#delTable td button.delete').click(function()
        {
            if (confirm("Are you sure you want to delete this row?"))
            {
                var id = $(this).parent().parent().attr('id');
                var parent = $(this).parent().parent();
                $.ajax(
                    {
                        type: "POST",
                        url: "<?php echo base_url('Welcome/delete');?>",
                        data:'id='+id,
                        cache: false,
                        success: function()
                        {
                            parent.fadeOut('slow', function()
                            {$(this).remove();});
                        }
                    });
            }
        });
        $('table#delTable tr:odd').css('background',' #FFFFFF');


    });

    function update(str){
        var id=str;
        var nm=$('#nm'+str).val();
        var em=$('#em'+str).val();
        var st=$('#st'+str).val();
        var ph=$('#ph'+str).val();
        var dp=$('#dp'+str).val();
        var un=$('#un'+str).val();

        var datas="id="+id+"&nm="+nm+"&em="+em+"&st="+st+"&ph="+ph+"&dp="+dp+"&un="+un;

        $.ajax(
            {
                type: "POST",
                url: "<?php echo base_url('Welcome/update');?>,
                data:datas,
                cache: false,
                success: function(msg) {
                    alert(msg);
                }
            });

    }


</script>

<button type="button" class="delete>Delete</button>
<button type="button" onclick="update(<?php echo $row['id']; ?>)">Save</button>

Controller

public function update(){
      $id=$_POST['id'];
      $userName=$_POST['nm'];
      $tokens=explode("",$userName);
      $fName=$tokens[0];
      $lName=$tokens[1];

      $userEmail=$_POST['em'];
      $userUni=$_POST['un'];
      $userState=$_POST['st'];
      $userDept=$_POST['dp'];
      $userPh=$_POST['ph'];
      $array = array(
          'first_name' => $fName,
          'last_name' => $lName ,
          'email' => $userEmail,
          'phone_number' => $userPh,
          'varsity_name' => $userUni,
          'state' => $userState,
          'dept_name'=> $userDept,
      );

          $this->load->model('Prime_Model');
          $result=$this->Prime_Model->updateProfile($id,$array);
      if($result){
          return "Data has updated";
      }
      else{
          return "Nothing";
      }
   }

You miss double quote after ?> below:

 $.ajax({
    type: "POST",
    url: "<?php echo base_url('Welcome/update');?>", // here
    data: datas,
    cache: false,
    success: function(msg) {
        alert(msg);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM