• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ajax php 实现写入数据库_php技巧

php 搞代码 3年前 (2022-01-26) 6次浏览 已收录 0个评论

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

<!---ecms -ecms  <BR>To change this template, choose Tools | Templates <BR>and open the template in the editor. <BR>--> <BR> <BR> <BR> <BR><title></title> <BR><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <BR><script src="submit.js" language="javascript"></script> <BR> <BR><body> <BR>Insert 知识点 <BR> <BR><label for="question"></label>知识点 <BR> <BR><br /><br /> <BR><label for="answer"> 答案</label> <BR> <BR><br /> <BR><br /> <BR> <BR> <BR> <BR><BR>


需要js来处理提交数据到服务器上以及从服务器获取提交后的返回数据. submit.js代码如:

/* <BR>* To change this template, choose Tools | Templates <BR>* and open the template in the editor. <BR>*/ <BR>var xmlHttp; <BR>function getValue(){ <BR>alert("getvaluel"); <BR>var question =document.insertForm.question.value; <BR>// alert(question); <BR>var answer = document.insertForm.answer.value; <BR>// alert(answer); <BR>submit(question,answer); <BR>}; <BR>function submit(question,answer){ <BR>xmlHttp=GetXmlHttpObject(); <BR>if (xmlHttp==null) <BR>{ <BR>alert ("Your browser does not support AJAX!"); <BR>return; <BR>} <BR>xmlHttp.onreadystatechange =function(){ <BR>if(xmlHttp.readyState ==4){ <BR>alert(xmlHttp.responseText); <BR>} <BR>}; <BR>var url = "insert1.php"; <BR>xmlHttp.open("post",url,true); <BR>xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8"); <BR>xmlHttp.send("question="+question+"&answer="+answer); <br><br>} <BR>function GetXmlHttpObject() <BR>{ <BR>var xmlHttp=null; <BR>try <BR>{ <BR>// Firefox, Opera 8.0+, Safari <BR>xmlHttp=new XMLHttpRequest(); <BR>} <BR>catch (e) <BR>{ <BR>// Internet Explorer <BR>try <BR>{ <BR>xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); <BR>} <BR>catch (e) <BR>{ <BR>xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); <BR>} <BR>} <BR>return xmlHttp; <BR>}<BR>


然后PHP处理界面,负责跟服务器交换数据

<?php <BR>/* <BR>* To change this template, choose Tools | Templates <BR>* and open the template in the editor. <BR>*/ <BR>//echo $_POST["question"]; <BR>//echo $_POST["answer"]; <BR>$q =$_POST['question']; <BR>$a = $_POST['answer']; <BR>//$q='qq'; <BR>//$a="a"; <BR>$con = mysql_connect("localhost","joe","123"); <BR>if (!$con) <BR>{ <BR>//die('Could not connect: ' . my<mark>@本文来源gaodaimacom搞#代%码@网-</mark><strong>搞代gaodaima码</strong>sql_error()); <BR>echo 'Could not connect: ' . mysql_error(); <BR>} <BR>mysql_select_db("joe",$con); <BR>mysql_query("INSERT INTO message VALUES ('$q', '$a', '无')"); <BR>mysql_close($con); <BR>echo "输入成功"; <BR>?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ajax php 实现写入数据库_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址