gpt4 book ai didi

javascript - codeigniter版本如何在不刷新网页的情况下插入并显示记录?

转载 作者:行者123 更新时间:2023-11-28 07:35:39 25 4
gpt4 key购买 nike

我这里有一个代码,可以使用 ajax 和纯 php 插入和显示记录,而无需刷新网页,但我不知道如何使用 codeigniter 进行设置。请帮忙。这是代码

插入.php

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js">
</script>
<script type="text/javascript" >
$(function() {
$(".comment_button").click(function() {

var test = $("#content").val();
var dataString = 'content='+ test;

if(test=='')
{
alert("Please Enter Some Text");
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" align="absmiddle">
<span class="loading">Loading Comment...</span>');

$.ajax({
type: "POST",
url: "demo_insert.php",
data: dataString,
cache: false,
success: function(html){
$("#display").after(html);
document.getElementById('content').value='';
document.getElementById('content').focus();
$("#flash").hide();
}
});
} return false;
});
});
</script>
// HTML code
<div>
<form method="post" name="form" action="">
<h3>What are you doing?</h3>
<textarea cols="30" rows="2" name="content" id="content" maxlength="145" >
</textarea><br />
<input type="submit" value="Update" name="submit" class="comment_button"/>
</form>
</div>
<div id="flash"></div>
<div id="display"></div>

demo_insert.phpPHP 代码显示最近从数据库插入的记录。

<?php
include('db.php');
if(isSet($_POST['content']))
{
$content=$_POST['content'];
mysql_query("insert into messages(msg) values ('$content')");
$sql_in= mysql_query("SELECT msg,msg_id FROM messages order by msg_id desc");
$r=mysql_fetch_array($sql_in);
}
?>
<b><?php echo $r['msg']; ?></b>

最佳答案

在您的wellcome Controller 中,您可以添加以下内容:

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

public function process()
{
$content=$this->input->post('content');
if($this->db->insert('mytable', array('msg' => $content))){
echo "<b>{$content}</b>";
}

然后,您应该在 application/views 中使用 inserting.php 作为 View ,并且 ajax url 将是 /process

没有测试过,但这应该可以解决问题。另外,您应该检查此示例 http://runnable.com/UXczcazDrMMiAAGl/how-to-do-ajax-in-codeigniter-for-php

关于javascript - codeigniter版本如何在不刷新网页的情况下插入并显示记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28623874/

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