如何使用PHP和jQuery将DIV标签保存到MySQL [英] How to save DIV tag to MySQL with PHP and jQuery

查看:61
本文介绍了如何使用PHP和jQuery将DIV标签保存到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using jQuery + PHP + MySQL My problem is how to save for example this div in my MySQL database using an Ajax call with jQuery to PHP.

PS.我知道如何使用jQuery将Ajax调用发送到PHP并将该调用中的变量保存到MySQL.

PS. i know how to send an Ajax call with jQuery to PHP and save variables from this call to 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.

This sends the contents of the div to the PHP script myscript.php.

myscript.php将具有这样的代码.

myscript.php will have code like this.

// 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和jQuery将DIV标签保存到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆