php - 如何使用 PHP 和 jQuery 将 DIV 标签保存到 MySQL

标签 php jquery mysql

我正在使用 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/

相关文章:

php - 如何有效地处理用户角色?

javascript - 在 JSON 数组中回显变量

javascript - 打印 <c :foreach> individually using javascript 的每个循环

jquery - 在新添加的项目上查找 li

php - 如何从动态数组中提取变量并创建更新查询?

mysql - Magento+ 添加到购物车不起作用

php Extract 对此图像的最佳猜测来自谷歌图像搜索?

php - 在准备好的语句中设置

java - 在 Hibernate 中使用 MySQL GET_LOCK 函数执行查询

php - 使用 SMTP 身份验证时通过 PEAR 发送 HTML 消息返回错误