ajax php 实现写入数据库
2015-01-24信息快讯网
<!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="submit.js" language="javascript"></script> </head> <body> Insert 知识点 <form name="insertForm"> <label for="question"></label>知识点 <input name="question" type="text"/> <br/><br/> <label for="answer"> 答案</label> <input name="answer" type="text"/> <br/> <br/> <input name="confirm" value="添加" type="button" onclick="getValue();"> </form> </body> </html>
需要js来处理提交数据到服务器上以及从服务器获取提交后的返回数据. submit.js代码如:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
var xmlHttp;
function getValue(){
alert("getvaluel");
var question =document.insertForm.question.value;
// alert(question);
var answer = document.insertForm.answer.value;
// alert(answer);
submit(question,answer);
};
function submit(question,answer){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
xmlHttp.onreadystatechange =function(){
if(xmlHttp.readyState ==4){
alert(xmlHttp.responseText);
}
};
var url = "insert1.php";
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
xmlHttp.send("question="+question+"&answer="+answer);
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
然后PHP处理界面,负责跟服务器交换数据
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//echo $_POST["question"];
//echo $_POST["answer"];
$q =$_POST['question'];
$a = $_POST['answer'];
//$q='qq';
//$a="a";
$con = mysql_connect("localhost","joe","123");
if (!$con)
{
//die('Could not connect: ' . mysql_error());
echo 'Could not connect: ' . mysql_error();
}
mysql_select_db("joe",$con);
mysql_query("INSERT INTO message VALUES ('$q', '$a', '无')");
mysql_close($con);
echo "输入成功";
?>
PHP学习之数组的定义和填充
PHP学习之正则表达式
PHP学习之字符串比较和查找
PHP学习之整理字符串
PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
PHP的cURL库功能简介 抓取网页、POST数据及其他
一个基于PDO的数据库操作类
PHP+JS+rsa数据加密传输实现代码
PHP学习笔记之三 数据库基本操作
PHP JSON格式数据交互实例代码详解
用php将任何格式视频转为flv的代码
php 正则表达式小结
JieqiCMS 杰奇1.5注册机php源码
自动把纯文本转换成Web页面的php代码
PHP高级OOP技术演示
php 获取可变函数参数的函数
php xml留言板 xml存储数据的简单例子
数据库查询记录php 多行多列显示
PHP 实现多服务器共享 SESSION 数据
通过PHP CLI实现简单的数据库实时监控调度
PHP 数据库 常见问题小结第1/3页