duangu1878 2019-04-06 17:25
浏览 70

如何在codeigniter中通过ajax post提交表单数据时停止重定向到控制器页面

I'm trying to submit form data from view to controller using Ajax post and return success message.And ajax in my view send data to function in controller successfully but controller function does not return response back to ajax success function.

my Controller - profile.php

function save()
 {
      $response = array();
     $this->form_validation->set_rules('fname', 'Title cant be empty ', 'required');
     if ($this->form_validation->run() == TRUE)
        {
             $param = $this->input->post();
             unset($param['submit']);
             $param['rid']=$this->session->userdata('id');  

            $profile_id = $param['profile_id'];
            unset($param['profile_id']);
            if($profile_id == 0)
            {

             $inserts = $this->profile_model->insert($param);
             if( $inserts>0){
                 $response['status'] = TRUE;
                 $response['notif'] ="success add profile details";
             }else{
                    $response['status'] = FALSE;
                    $response['notif']  = 'Server wrong, please save again';
            }
            }else{

            }

     }else{
            $response['status'] = FALSE;
            $response['notif']  = validation_errors();
     }
 echo json_encode($response);

 } 

view - profile_new_user.php

             ?>
    <form method="POST" id="form_profile" action="<?php echo base_url().'profile/save'; ?>">
     <input type="hidden" name="profile_id" value="0"><!-------event id---------->
     <div class="form-group">

                        <label>Prefix</label>
                         <select name="prefix" class="form-control">
.
.
.
                     </div>
     <div class="form-group">
      <input type="submit" name="submit" value="Save" class="btn btn-info" />
     </div>
    </form>
 <script type="text/javascript">
 $(document).ready(function() {

 $("#form_profile").submit(function(event){
     event.preventDefault(); 
    $.ajax({
  method: "POST",
  url: $(this).attr("action"),
  dataType :'JSON',
  data: $(this).serialize(),
   success: function(data)
   { 

   if(data.status){   
       alert('ok');
   }else{
        alert('not ok');
   }
   }
})
});
   });

model - profile_model.php

function insert($data)
 {
  $this->db->insert('user_profile', $data);
  return $this->db->insert_id();
 }

after I click submit button page redirect to url - http://localhost/ptm6/profile/save and page shows

{"status":true,"notif":"success add profile details"}

in plain text.

I expect to show alert message without any redirection.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
    • ¥15 数学的三元一次方程求解
    • ¥20 iqoo11 如何下载安装工程模式
    • ¥15 本题的答案是不是有问题
    • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
    • ¥15 C++使用Gunplot
    • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
    • ¥15 matlab数字图像处理频率域滤波
    • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
    • ¥15 ELGamal和paillier计算效率谁快?