【问题标题】:How to save DIV tag to MySQL with PHP and jQuery如何使用 PHP 和 jQuery 将 DIV 标签保存到 MySQL
【发布时间】:2011-03-08 16:56:52
【问题描述】:

我正在使用 jQuery + PHP + MySQL 我的问题是如何使用 jQuery 到 PHP 的 Ajax 调用将这个 div 保存在我的 MySQL 数据库中。

PS。我知道如何使用 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>

【问题讨论】:

  • 你已经有了服务器端代码,只想要js,还是两者都需要?
  • 所以你真的在问“如何使用 jQuery 获取 div 的内容?”
  • @mark 我已经使用 jQuery 和 ajax 在 pasr 中将许多变量发送到 PHP,并且我能够保存到 MySQL。

标签: php jquery mysql


【解决方案1】:

使用 php mysql 格式化程序。

假设这是你的变量$yourContent

使用mysql_real_escape_string($yourContent)函数转换它

【讨论】:

  • 谢谢你的帮助我会试试这个功能。
【解决方案2】:
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 :(
}

【讨论】:

  • 在尝试从我的数据库中检索此 div 内容并将其发送到其他 PHP 脚本以附加到
    时,我是否需要反向使用 mysql_real_escape_string() 函数
  • 您不需要对来自数据库的数据调用它,只需对进入数据库的数据调用。我建议您阅读 SQL 注入以了解您需要该函数的原因以及它的作用。
  • 感谢您的回复。我会试试这个功能。
  • 有没有办法做到这一点,而无需 php im 尝试做类似的事情,但使用 aspx 页面
猜你喜欢
相关资源
最近更新 更多
热门标签