javascript - 重新加载div而不刷新整个页面

标签 javascript php html mysql ajax

我已经将 mysql 数据库与表单内的 div 容器连接起来。但是当 mysql 数据库中有新元素时,我必须重新加载整个页面才能在 div 中显示新元素。

div 的代码:

<form method="post" action="startseite.php">
<div id="message">
<input type="text" name="spind" value="" size="55" maxlength="90">
<button>Senden</button>
</div></form>

我的 div 容器的 Javascript 代码:

<script type="text/javascript">
$(document).ready(function(){

    $("#content").scrollTop($("#content")[0].scrollHeight);     
    $("button").click(function(){
    var message = $("input").val();
    var old = $("#content").html();

    $("#content").scrollTop($("#content")[0].scrollHeight);

    }); 
});

mysql数据库代码:

<?php
$verbindung = mysql_connect("localhost", "hallo", "123")
or die("Fehler im System");

mysql_select_db("hallo")
or die("Verbindung zur Datenbank war nicht erfolgreich");


$user = $_SESSION['username'];
$message = $_POST['spind'];

$eintrag = "INSERT INTO chat
(user, message)

VALUES
('$user', '$message')";

mysql_query($eintrag);

mysql_close($verbindung);
?>

我读到这可以通过 AJAX 来完成,但也许还有另一种方法可以在不使用 AJAX 的情况下完成此操作。

最佳答案

您可以创建一个 resfull api,仅返回数据,然后使用 jquery ajax 进行调用并添加新元素

http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/ http://www.ibm.com/developerworks/web/library/wa-ajaxservice/index.html?ca=dat 教程

关于javascript - 重新加载div而不刷新整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23958899/

相关文章:

php - Wordpress 循环不显示所有帖子

PHP - MySQL DB 从连接表中选择,显示具有重复名称的列?

html - CSS 媒体查询 - 视口(viewport)宽度被 iPhone 忽略

javascript - 在 javascript 中为图形传递值

javascript - 在文本字段中使用 mobiscroll 多选时出现奇怪的按钮

javascript - 相对于父 div 之外的位置,没有滚动但可见

php - 更新和删除 CakePHP 中的唯一连接关系

javascript - 如何在 node.js 中使用 soap webservices

javascript - 如何在javascript中显示选定的元素

javascript - 并排放置 div 失败