手机
当前位置:查字典教程网 >编程开发 >php教程 >ajax php 实现写入数据库
ajax php 实现写入数据库
摘要:首先需要一个带输入表格.复制代码代码如下:Insert知识点知识点答案需要js来处理提交数据到服务器上以及从服务器获取提交后的返回数据.su...

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

复制代码 代码如下:

<>

<!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">

</form>

</body>

</html>

需要js来处理提交数据到服务器上以及从服务器获取提交后的返回数据. 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处理界面,负责跟服务器交换数据

复制代码 代码如下:

<?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 "输入成功";

?>

【ajax php 实现写入数据库】相关文章:

php+ajax实现无刷新动态加载数据技术

php简单操作mysql数据库的类

用PHP实现XML备份Mysql数据库

PHP教程:实现的将图片转换为TXT

用PHP调用数据库的存贮过程!

php实现window平台的checkdnsrr函数

php实现插入排序

php实现求相对时间函数

php实现读取内存顺序号

简单易用的计数器(数据库)

精品推荐
分类导航