php - 无法在codeigniter中使用ajax将数据保存在数据库中
问题描述
我正在使用 Codeigniter 框架并且无法在我试图将数据保存在 MySql 中的 codeigniter 中使用 ajax 将数据保存在数据库中。
我正在使用 AJAX 传递数据,但表单数据没有保存并显示来自 AJAX 错误函数的错误Some error。
下面是我的代码:
意见/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);
}
}
模型/UserModel.php
class UserModel extends CI_Model{
public function saveData($data){
if($this->db->insert('users',$data)){
return 1;
}
else{
return 0;
}
}
}
有人请让我知道代码有什么问题。任何帮助将不胜感激。
谢谢
解决方案
这是最终的解决方案,并且工作正常。
意见/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";
}
}
}
模型/UserModel.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";
}
}
}
?>
谢谢
推荐阅读
- android - frida hook 原生非导出函数
- python - 如何通过 Heroku 上的 python 代码更新环境变量?
- podman - 更改现有 podman 网络子网的正确方法
- desktop-application - 使用 WinAppDriver 自动执行 Windows 应用程序安装过程时显示未知错误
- java - kafka消费者延迟,使用spring cloud stream listner
- wpf - 如何删除listview wpf列之间的空格?
- sql - 过程中的 PL/SQL DBMS 输出不显示
- reactjs - 将 objectFit 与 gatsby-plugin-image 中的 StaticImage 结合使用
- python - conda 环境使用/调用不正确的 python 版本
- discord - 我如何使用用户 ID 而不是在 discord.js 上提及来赋予角色?