首先需要一个带输入表格.
复制代码代码如下:
-//W3C//DTDHTML4.01Transitional//EN">
Insert知识点
知识点
答案
需要js来处理提交数据到服务器上以及从服务器获取提交后的返回数据.submit.js代码如:
复制代码代码如下:
/*
*Tochangethistemplate,chooseTools|Templates
*andopenthetemplateintheeditor.
*/
varxmlHttp;
functiongetValue(){
alert("getvaluel");
varquestion=document.insertForm.question.value;
//alert(question);
varanswer=document.insertForm.answer.value;
//alert(answer);
submit(question,answer);
};
functionsubmit(question,answer){
xmlHttp=GetXmlHttpObject();
if(xmlHttp==null)
{
alert("YourbrowserdoesnotsupportAJAX!");
return;
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
alert(xmlHttp.responseText);
}
};
varurl="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);
}
functionGetXmlHttpObject()
{
varxmlHttp=null;
try
{
//Firefox,Opera8.0+,Safari
xmlHttp=newXMLHttpRequest();
}
catch(e)
{
//InternetExplorer
try
{
xmlHttp=newActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
}
}
returnxmlHttp;
}
然后PHP处理界面,负责跟服务器交换数据
复制代码代码如下:
/*
*Tochangethistemplate,chooseTools|Templates
*andopenthetemplateintheeditor.
*/
//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('Couldnotconnect:'.mysql_error());
echo'Couldnotconnect:'.mysql_error();
}
mysql_select_db("joe",$con);
mysql_query("INSERTINTOmessageVALUES('$q','$a','无')");
mysql_close($con);
echo"输入成功";
?>
您可能感兴趣的文章:php从数据库中获取数据用ajax传送到前台的方法php+ajax实现输入读取数据库显示匹配信息AjaxPHP边学边练之三数据库PHPjQuery+Ajax结合写批量删除功能php+ajax实现图片文件上传功能实例php的ajax简单实例php+ajax实现无刷新动态加载数据技术php采用ajax数据提交post与post常见方法总结ThinkPHP中使用ajax接收json数据的方法php+ajax+jquery实现点击加载更多内容PHP+jQuery+Ajax实现用户登录与退出php+ajax实现的写入数据库操作简单示例