Php 在不刷新页面的情况下,如何使用ajax/jQuery显示数据库中的值
在通过jQuery/ajax将数据插入数据库后,在不刷新页面的情况下从数据库中获取值时,如何使用codeigniter显示数据库值 这是我的代码: 脚本:Php 在不刷新页面的情况下,如何使用ajax/jQuery显示数据库中的值,php,jquery,html,ajax,codeigniter,Php,Jquery,Html,Ajax,Codeigniter,在通过jQuery/ajax将数据插入数据库后,在不刷新页面的情况下从数据库中获取值时,如何使用codeigniter显示数据库值 这是我的代码: 脚本: <script> $(document).ready(function(){ $("#personal-info").submit(function(e){ e.preventDefault(); var suppid = $(
<script>
$(document).ready(function(){
$("#personal-info").submit(function(e){
e.preventDefault();
var suppid = $("#suppid").val();
var proid = $("#proid").val();
var custid = $("#custid").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/Profile_cntrl/buyer_communication",
data: {suppid:suppid,proid:proid,custid:custid,message:message},
success:function(data)
{
alert('SUCCESS!!');
},
error:function()
{
alert('fail');
}
});
});
});
</script>
型号:
function buyer_insert($data4) {
$this->db->insert('communication', $data4);
return ($this->db->affected_rows() != 1) ? false : true;
}
表格:
@Maruthi Prasad这是密码。。[在代码点火器中]
带有jquery脚本的HTML视图代码
views\profile\u view.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<div id="load_data">
</div>
<form method="post" id="personal-info">
<input id="message" name="message" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." />
<button type="submit" class="btn btn-primary btn-sm" id="submit-p" name="submit-p">Send</button>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function(){
loaddata();
data_submit();
});
function loaddata(){
$.getJSON('<?php echo base_url();?>'+'index.php/Profile_cntrl/get_data',function(data){
for(var i in data){
var show = '<div>';
show += '<h5 style="background:#ccc;padding:10px;border-radius:10px;">'+data[i].message+'</h5>';
show += '</div>';
$("#load_data").append(show);
}
});
}
function data_submit(){
$("#personal-info").submit(function(e){
e.preventDefault();
var formdata = $(this).serialize();
$.ajax({
type:'POST',
url:'<?php echo base_url();?>'+'index.php/Profile_cntrl/insert_data',
data:formdata,
success:function(data){
var res = JSON.parse(data);
if(res.Status == 'true'){
//console.log(res.report);
$("#load_data").empty();
loaddata()
}else{
alert(res.report);
}
}
});
});
}
</script>
</body>
</html>
引导示例
发送
$(文档).ready(函数(){
loaddata();
数据提交();
});
函数loaddata(){
$.getJSON(“”+”index.php/Profile\u cntrl/get\u data',函数(数据){
用于(数据中的var i){
var show='';
显示+=''+数据[i]。消息+'';
显示+='';
$(“#加载#u数据”)。追加(显示);
}
});
}
函数数据_提交(){
$(“#个人信息”)。提交(功能(e){
e、 预防默认值();
var formdata=$(this.serialize();
$.ajax({
类型:'POST',
url:“+”index.php/Profile\u cntrl/insert\u data',
数据:formdata,
成功:功能(数据){
var res=JSON.parse(数据);
如果(res.Status='true'){
//控制台日志(res.report);
$(“#加载#数据”).empty();
loaddata()
}否则{
警报(res.report);
}
}
});
});
}
控制器代码:
控制器\Profile\u cntrl.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
header('Access-Control-Allow-Origin: *');
class Profile_cntrl extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('profile_model');
$this->load->helper(array('url','html','form'));
}
public function index()
{
$this->load->view('profile_view');
}
public function get_data(){
$query = $this->profile_model->buyer_communication();
echo json_encode($query);
}
public function insert_data(){
$arr = array(
'message'=>$this->input->post('message')
);
$sql = $this->profile_model->buyer_insert($arr);
$op = "data insert id :".$this->db->insert_id();
if($sql == true){
$reponse = array(
'Status'=>'true',
'report'=>$op
);
echo json_encode($reponse);
}
else
{
$op = "Failed to insert data";
$reponse = array(
'Status'=>'false',
'report'=>$op
);
echo json_encode($reponse);
}
}
}
?>
型号代码:
models\Profile\u model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Profile_model extends CI_model {
public function buyer_communication(){
$sql = $this->db->get('communication');
$sql = $sql->result_array();
return $sql;
}
public function buyer_insert($arr){
return $query = $this->db->insert('communication',$arr);
}
}
?>
请随意提问,你们两个是在做同一件事吗?最好在同一问题上只提一个问题。另外,请让你的朋友接受另一个答案,因为它解决了原来的问题。不管怎样,你没有正确地描述这一点。你的代码出了什么问题?它在哪一点上失败了?你有什么错误,如果有的话?什么行为是错误的?另外,为什么在ajax中,当您可以序列化整个表单时,您要手动从表单字段中提取值<代码>数据:$(this).serialize()
应该可以工作,而不是所有那些重复的变量声明。在向数据库中插入值,同时从数据库中获取值而不刷新之后,页面值应显示在表单中。表单代码也只添加了一次,请参考并建议meLet us。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
header('Access-Control-Allow-Origin: *');
class Profile_cntrl extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('profile_model');
$this->load->helper(array('url','html','form'));
}
public function index()
{
$this->load->view('profile_view');
}
public function get_data(){
$query = $this->profile_model->buyer_communication();
echo json_encode($query);
}
public function insert_data(){
$arr = array(
'message'=>$this->input->post('message')
);
$sql = $this->profile_model->buyer_insert($arr);
$op = "data insert id :".$this->db->insert_id();
if($sql == true){
$reponse = array(
'Status'=>'true',
'report'=>$op
);
echo json_encode($reponse);
}
else
{
$op = "Failed to insert data";
$reponse = array(
'Status'=>'false',
'report'=>$op
);
echo json_encode($reponse);
}
}
}
?>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Profile_model extends CI_model {
public function buyer_communication(){
$sql = $this->db->get('communication');
$sql = $sql->result_array();
return $sql;
}
public function buyer_insert($arr){
return $query = $this->db->insert('communication',$arr);
}
}
?>