CODEIGNITER AJAX 'myForm'));?> EM-6ren">
gpt4 book ai didi

php - Codeigniter 和 jquery Ajax

转载 作者:可可西里 更新时间:2023-10-31 23:01:43 24 4
gpt4 key购买 nike

我无法使用ajax在数据库中通过表单插入数据,没有firebug错误有人可以帮助我

查看:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
<link rel="stylesheet" href="<?php echo base_url("assets/css/bootstrap.min.css"); ?>">
</head>
<body>
<h3 style="text-align: center;">CODEIGNITER AJAX</h3>
<div class="row">
<div class="alert alert-success" id="message" style="display: none;">
</div>
<div class="col-md-4"></div>
<div class="col-md-4">
<?php echo form_open('blog_c',array('id'=>'myForm'));?>
<div class="form-group">
<label>EMAIL:</label>
<input type="text" name="email" id="email" class="form-control" placeholder="EMAIL">
</div>
<input type="submit" value="ENVOYER" id="btn">
<?php echo form_close()?>
</div>
</div>
<script type="text/javascript" src="<?php echo base_url()?>assets/js/jquery-3.1.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#btn').click(function(){
var email=$('#email').val();
$.ajax({ //request ajax
url:"<?php echo site_url('blog_c/registre')?>",
type:POST,
data:{email:email},
dataType:json,
success: function(repons) {
$("#message").html(repons);

},
error: function() {
alert("Invalide!");
}
});
});
});

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

型号:

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

class Blog_m extends CI_Model
{
function __construct()
{
parent:: __construct();
}
function registre($data)
{
$this->db->insert('utilisateurs',$data);
}

}

Controller :

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

class Blog_c extends CI_Controller
{
public function __construct()
{
parent::__construct();

}

public function index()
{
$this->load->view('blog_w');
}

public function registre()
{
// set rules
$this->form_validation->set_rules('email','EMAIL','trim|required|valid_email|is_unique[utilisateurs.email]');
if($this->form_validation->run()==FALSE)
{
echo validation_errors();
}else{
$data=array(
'email'=>$this->input->post('email'));
$this->blog_m->registre($data);


echo "<div class='alert'>Inscription success</div>";
echo "email";
}
}

}

没有报错,但是数据没有插入数据库,也没有成功信息。

最佳答案

试试这个。

在 View 中(AJAX 部分)

<script>
$(function(){
$( "#btn" ).click(function(event)
{
event.preventDefault();
var email= $("#email").val();

$.ajax(
{
type:"post",
url: "<?php echo base_url(); ?>index.php/blog_c/registre",
data:{ email:email},
success:function(response)
{
console.log(response);
$("#message").html(response);
$('#cartmessage').show();
}
error: function()
{
alert("Invalide!");
}
}
);
});
});
</script>

在 Controller 中

public function registre()
{

$email = $this->input->post('email'); # add this

$this->form_validation->set_rules('email','EMAIL','trim|required|valid_email|is_unique[utilisateurs.email]');

if($this->form_validation->run() == FALSE)
{
echo validation_errors();
}
else
{
if(!$this->blog_m->registre($email))
{
echo "Something Went Wrong";
}
else
{
echo "Inscription success";
}

}
}

在模型中

function registre($email)
{
$data = array(
'email'=>$this->input->post('email')
);

$this->db->insert('utilisateurs',$data);
}

关于php - Codeigniter 和 jquery Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42006000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com