ajax php 實(shí)現(xiàn)寫入數(shù)據(jù)庫
更新時(shí)間:2009年09月02日 01:33:47 作者:
看到同學(xué)背詞用的生詞本,覺得很不錯(cuò).正好自己也在看書,為了有一個(gè)好的效果,于是想做一個(gè)類似生詞本的,可以存知識(shí)點(diǎn),和查看知識(shí)點(diǎn)的.
首先需要一個(gè)帶輸入表格.
<!--
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 知識(shí)點(diǎn)
<form name="insertForm">
<label for="question"></label>知識(shí)點(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 知識(shí)點(diǎn)
<form name="insertForm">
<label for="question"></label>知識(shí)點(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傳送到前臺(tái)的方法
- 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)無刷新動(dòng)態(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連接Access數(shù)據(jù)庫的方法小結(jié)
有時(shí)候我們?cè)陂_發(fā)一些小程序或簡單功能的管理系統(tǒng),又沒有mysql數(shù)據(jù)庫的情況下,就可以使用access了,這里介紹下鏈接方法2013-06-06
PHP實(shí)現(xiàn)通過get方式識(shí)別用戶發(fā)送郵件的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)通過get方式識(shí)別用戶發(fā)送郵件的方法,涉及php針對(duì)數(shù)據(jù)庫的讀取、判斷及session登陸的使用技巧,需要的朋友可以參考下2015-07-07
本地計(jì)算機(jī)無法啟動(dòng)Apache故障處理
安裝了WMAP后,一直無法運(yùn)行成功。后發(fā)現(xiàn)Apache一直都不在運(yùn)行狀態(tài)。到WMAP中的Apache選項(xiàng)中去選擇啟動(dòng)但無法啟動(dòng)。下面總結(jié)下處理此問題的幾種思路方法2014-08-08
php實(shí)現(xiàn)壓縮合并js的方法【附demo源碼下載】
這篇文章主要介紹了php實(shí)現(xiàn)壓縮合并js的方法,涉及php壓縮文件類JSMin的相關(guān)調(diào)用與使用技巧,并附帶了完整的demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09

