无法在CodeIgniter中使用 AJAX 保存数据库中的数据

mw3dktmi  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(189)

我正在使用CodeIgniter框架,但无法将数据保存在MySQL数据库中。
我正在使用 AJAX 传递数据,但表单数据未保存,并显示AJAX错误函数某些错误
下面是我的代码:

浏览次数/test.php

<div class="container">
        
        <h1 align="center">Ajax Form using CI</h1>

        <div class="form-group">
            
            <input type="text" class="form-control" id="name" name="name" placeholder="Name"/>
            <input type="text" class="form-control" id="age" name="age" placeholder="Age"/>
            <input type="button" class="btn btn-primary" id="save" value="SAVE DATA"> 

        </div>

    </div>

   <script type="text/javascript">

      $(document).ready(function(){

           $("#save").click(function(){

                   var name = $("#name").val();
                   var age = $("#age").val();

                   if(name != "" && age != ""){

                      $.ajax({
                        
                        type:"POST",
                        url:"<?php echo base_url('/index.php/Welcome/test'); ?>",
                        dataType: 'html',
                        data:{name:name,age:age},
                        success:function(res){
                            
                            if(res==1){
      
                                alert('Data saved successfully');   
                             }
                            else{
        
                                alert('Data not saved');    
                             }
                        },
                        error:function(){
                            alert('Some error');
                        }  

                      });
                   }
                   else{
                     alert("Field is empty");
                   }
          });
      });

   </script>

控制器/welcome.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function loadForm(){

    $this->load->view('test');
}

public function test(){

    $data = array(

       'name'=> $this->input->post('name'),
       'age'=> $this->input->post('age')
    );

    $this->load->model('UserModel');
    $result = $this->UserModel->saveData($data);
  }
}

模型/用户模型.php

class UserModel extends CI_Model{

   public function saveData($data){

      if($this->db->insert('users',$data)){
              return 1;
          }
          else{
          return 0;
        }
      }
  }

密码有什么问题?

7gyucuyw

7gyucuyw1#

代替

error:function(){
    alert('Some error');
  }

下面是一个例子:

error: function(xhr, textStatus, error) {

 console.log("readyState: " + xhr.readyState);
 console.log("responseText: "+ xhr.responseText);
 console.log("status: " + xhr.status);
 console.log("text status: " + textStatus);
 console.log("error: " + error);

}
bis0qfac

bis0qfac2#

这是最终的解决方案,运行良好。

浏览次数/test.php

<!doctype html>
<html class="no-js" lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Register using AJAX</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="apple-touch-icon" href="icon.png">
    <!-- Place favicon.ico in the root directory -->

   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <style>

       #age,.btn{
            margin-top: 20px
        }
 
  </style>  

</head>
<body>
  
    <div class="container">
        
        <h1 align="center">Ajax Form using CI</h1>

        <div class="form-group">
            
            <input type="text" class="form-control" id="name" name="name" placeholder="Name"/>
            <input type="text" class="form-control" id="age" name="age" placeholder="Age"/>
            <input type="button" class="btn btn-primary" id="save" value="SAVE DATA"> 

        </div>

    </div>

   <script type="text/javascript">

      $(document).ready(function(){

           $("#save").click(function(){

                   var name = $("#name").val();
                   var age = $("#age").val();

                   if(name != "" && age != ""){

                      
                      $.ajax({
                        
                        type:"POST",
                        url:"<?php echo base_url();?>index.php/Welcome/test",
                        dataType: 'html',
                        data:{name:name,age:age},
                        success:function(response){
                            
                           alert(response);  
                        },
                        error:function(xhr, status, error){
                 
                        console.log("readyState: " + xhr.readyState);
                        console.log("responseText: "+ xhr.responseText);
                        console.log("status: " + xhr.status);
                        console.log("text status: " + textStatus);
                        console.log("error: " + error);
                        }  

                      });  
                   }
                   else{
                     alert("Field is empty");
                   }
          });
      });

   </script> 
  </body>
</html>

控制器/welcome.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function loadForm(){

    $this->load->view('test');
}

public function test(){

    $data = array(

       'name'=> $this->input->post('name'),
       'age'=> $this->input->post('age')
    );

    $this->load->model('UserModel');
    $result = $this->UserModel->saveData($data);

    if($result == "added"){
        echo "yeah success";
    }
    else{
        echo "failed";
    }
  }
}

模型/用户模型.php

<?php
  defined('BASEPATH') OR exit('No direct script access allowed');

class UserModel extends CI_Model{

public function saveData($data){

      if($this->db->insert('users',$data)){
              return "added";
          }
          else{
          return "not added";
         }    
      }
   }
 ?>

相关问题