首先需要一个带输入表格.
<!---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>