ajax php 實現(xiàn)寫入數(shù)據(jù)庫
更新時間:2009年09月02日 01:33:47 作者:
看到同學背詞用的生詞本,覺得很不錯.正好自己也在看書,為了有一個好的效果,于是想做一個類似生詞本的,可以存知識點,和查看知識點的.
首先需要一個帶輸入表格.
<!--
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來處理提交數(shù)據(jù)到服務器上以及從服務器獲取提交后的返回數(shù)據(jù). 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處理界面,負責跟服務器交換數(shù)據(jù)
<?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 "輸入成功";
?>
復制代碼 代碼如下:
<!--
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來處理提交數(shù)據(jù)到服務器上以及從服務器獲取提交后的返回數(shù)據(jù). 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處理界面,負責跟服務器交換數(shù)據(jù)
復制代碼 代碼如下:
<?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從數(shù)據(jù)庫中獲取數(shù)據(jù)用ajax傳送到前臺的方法
- php+ajax 實現(xiàn)輸入讀取數(shù)據(jù)庫顯示匹配信息
- Ajax PHP 邊學邊練 之三 數(shù)據(jù)庫
- PHP jQuery+Ajax結合寫批量刪除功能
- php+ajax實現(xiàn)圖片文件上傳功能實例
- php的ajax簡單實例
- php+ajax實現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術
- php采用ajax數(shù)據(jù)提交post與post常見方法總結
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- php+ajax+jquery實現(xiàn)點擊加載更多內(nèi)容
- PHP+jQuery+Ajax實現(xiàn)用戶登錄與退出
- php + ajax 實現(xiàn)的寫入數(shù)據(jù)庫操作簡單示例
相關文章
PHP實現(xiàn)通過get方式識別用戶發(fā)送郵件的方法
這篇文章主要介紹了PHP實現(xiàn)通過get方式識別用戶發(fā)送郵件的方法,涉及php針對數(shù)據(jù)庫的讀取、判斷及session登陸的使用技巧,需要的朋友可以參考下2015-07-07
php實現(xiàn)壓縮合并js的方法【附demo源碼下載】
這篇文章主要介紹了php實現(xiàn)壓縮合并js的方法,涉及php壓縮文件類JSMin的相關調(diào)用與使用技巧,并附帶了完整的demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09

