jQuery Ajax 插入数据库示例
wp_jquery_ajax_insert.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>jQery Ajax Insert Example</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="zh-CN" /> <script type="text/javascript" src="https://files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js"></script> <script type="text/javascript"> <!-- $(document).ready(function(){ $("#post_data").click(function(){ $.ajax({ url:"wp_jquery_ajax_insert_process.php", data:"data="+$("#text_data").val(), type:"POST", success:function(msg){ $("#feedback").empty().append(msg).show(); setTimeout('$("#feedback").empty().hide()',3000); } }); }); }); //--> </script> </head> <body> <div id="feedback"></div> <div> <textarea id="text_data"></textarea> <input id="post_data" type="button" value="提交"></input> </div> </body> </html>
wp_jquery_ajax_insert_process.php
<?php require_once 'db.php'; $post_text = mysql_real_escape_string($_POST['data'],$conn); if(!mysql_query("INSERT INTO `wp_jquery_ajax_insert`(text) VALUES('$post_text')",$conn)){ echo 'Failed!'.mysql_error($conn); }else { echo 'Success!'; } //End_php
表 wp_jquery_ajax_insert SQL
CREATE TABLE `wp_jquery_ajax_insert` ( `id` int(11) NOT NULL AUTO_INCREMENT, `text` varchar(1024) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
db.php
View Code
<?php static $connect = null; static $table = ''; if(!isset($connect)) { $connect = mysql_connect("localhost","root",""); if(!$connect) { $connect = mysql_connect("localhost","Zjmainstay",""); } if(!$connect) { die('Can not connect to database.Fatal error handle by /test/db.php'); } mysql_select_db("test",$connect); mysql_query("SET NAMES utf8",$connect); $conn = &$connect; $db = &$connect; }