ajax php 實(shí)現(xiàn)寫入數(shù)據(jù)庫
更新時間:2009年09月02日 01:33:47 作者:
看到同學(xué)背詞用的生詞本,覺得很不錯.正好自己也在看書,為了有一個好的效果,于是想做一個類似生詞本的,可以存知識點(diǎn),和查看知識點(diǎn)的.
首先需要一個帶輸入表格.
<!--
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 知識點(diǎn)
<form name="insertForm">
<label for="question"></label>知識點(diǎn)
<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ù)到服務(wù)器上以及從服務(wù)器獲取提交后的返回?cái)?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處理界面,負(fù)責(zé)跟服務(wù)器交換數(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 "輸入成功";
?>
復(fù)制代碼 代碼如下:
<!--
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 知識點(diǎn)
<form name="insertForm">
<label for="question"></label>知識點(diǎn)
<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ù)到服務(wù)器上以及從服務(wù)器獲取提交后的返回?cái)?shù)據(jù). submit.js代碼如:
復(fù)制代碼 代碼如下:
/*
* 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處理界面,負(fù)責(zé)跟服務(wù)器交換數(shù)據(jù)
復(fù)制代碼 代碼如下:
<?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 實(shí)現(xiàn)輸入讀取數(shù)據(jù)庫顯示匹配信息
- Ajax PHP 邊學(xué)邊練 之三 數(shù)據(jù)庫
- PHP jQuery+Ajax結(jié)合寫批量刪除功能
- php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
- php的ajax簡單實(shí)例
- php+ajax實(shí)現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
- php采用ajax數(shù)據(jù)提交post與post常見方法總結(jié)
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- php+ajax+jquery實(shí)現(xiàn)點(diǎn)擊加載更多內(nèi)容
- PHP+jQuery+Ajax實(shí)現(xiàn)用戶登錄與退出
- php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫操作簡單示例
相關(guān)文章
php基于 swoole 實(shí)現(xiàn)的異步處理任務(wù)功能示例
這篇文章主要介紹了php基于 swoole 實(shí)現(xiàn)的異步處理任務(wù)功能,涉及swoole的安裝、配置及php調(diào)用swoole守護(hù)進(jìn)程監(jiān)控任務(wù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08PHP+redis實(shí)現(xiàn)微博的拉模型案例詳解
這篇文章主要介紹了PHP+redis實(shí)現(xiàn)微博的拉模型案例,結(jié)合具體實(shí)例形式詳細(xì)分析了php+redis獲取關(guān)注人最新信息的相關(guān)原理與操作技巧,需要的朋友可以參考下2019-07-07PHP下利用shell后臺運(yùn)行PHP腳本,并獲取該腳本的Process ID的代碼
PHP下利用shell后臺運(yùn)行PHP腳本,并獲取該腳本的Process ID的代碼,學(xué)習(xí)php的朋友可以參考下。2011-09-09PHP中header()函數(shù)的七種用法小結(jié)
我們在實(shí)際開發(fā)中經(jīng)常使用header()實(shí)現(xiàn)一些功能,這篇文章介紹關(guān)于header()的7中用法,文中有詳細(xì)的代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-08-08