php ajax 直接改数据库,ajax php 实现写入数据库

首先需要一个带输入表格.

复制代码代码如下:

-//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实现的写入数据库操作简单示例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值