我正在使用 jQuery + PHP + MySQL 我的问题是如何使用 jQuery 到 PHP 的 Ajax 调用将这个 div 保存在我的 MySQL 数据库中。
附言。我知道如何使用 jQuery 向 PHP 发送 Ajax 调用并将此调用中的变量保存到 MySQL。
<div id="id-aaa"> Les ordinateurs, eux, comme on l’a vu, ont un dispositif physique fait pour stocker (de multiples façons) des informations binaires. Alors, lorsqu’on
représente une information stockée par un ordinateur, le plus <span id="99" value="simple est
d’utiliser " style="color: black; font-size: 14px; font-weight: bolder;">simple est
d’utiliser </span>un système de représentation à deux chiffres les fameux 0 et 1.
Mais une fois de plus, je me permets d’insister, le choix du 0 et du 1<font id="annotation n°=2" value=" est
une pure convention" style="background-color: rgb(255, 255, 0);"> est
une pure convention</font>, et on aurait pu <font id="annotation n°=1" value="choisir n’importe " style="border-bottom: medium solid rgb(51, 255, 51);">choisir n’importe </font>quelle autre paire de
symboles à leur place.
</div>
最佳答案
var div_contents = $("#id-aaa").html();
$.post('myscript.php', { contents: div_contents });
这会将 div 的内容发送到 PHP 脚本 myscript.php
。
myscript.php 会有这样的代码。
// Using MySQL lib here, as it's most widely recognised
// Replace with MySQLi functions/object if you prefer
$con = mysql_connect($host, $username, $password);
mysql_select_db($database, $con);
$div = mysql_real_escape_string($_POST['contents']); // Make sure to clean the
// data before putting SQL
$sql = "INSERT INTO divs (contents) VALUES ('{$div}')";
$query = mysql_query($sql, $con);
if($query) {
// Success!
} else {
// Failure :(
}
关于php - 如何使用 PHP 和 jQuery 将 DIV 标签保存到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3125295/