ajax php 实现写入数据库

前端之家收集整理的这篇文章主要介绍了ajax php 实现写入数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先需要一个带输入表格. @H_404_0@<div class="codetitle"><a style="CURSOR: pointer" data="75935" class="copybut" id="copybut75935" onclick="doCopy('code75935')"> 代码如下:

<div class="codebody" id="code75935">@H_4040@<!-- @H4040@To change this template,choose Tools | Templates @H4040@and open the template in the editor. @H4040@--> @H4040@<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> @H4040@ @H4040@ @H4040@ @H404_0@<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> @H_4040@<script src="submit.js" language="javascript"> @H4040@ @H4040@ @H4040@Insert 知识点 @H4040@<form name="insertForm"> @H4040@<label for="question">知识点 @H4040@<input name="question" type="text"/> @H4040@

@H
4040@<label for="answer"> 答案 @H4040@<input name="answer" type="text"/> @H4040@
@H
4040@
@H
404_0@<input name="confirm" value="添加" type="button" onclick="getValue();"> @H_4040@ @H4040@ @H4040@@H4040@
@H404_0@需要js来处理提交数据到服务器上以及从服务器获取提交后的返回数据. submit.js代码如: @H_404_0@<div class="codetitle"><a style="CURSOR: pointer" data="67649" class="copybut" id="copybut67649" onclick="doCopy('code67649')"> 代码如下:
<div class="codebody" id="code67649">@H_4040@/* @H4040@* To change this template,choose Tools | Templates @H4040@* and open the template in the editor. @H4040@*/ @H4040@var xmlHttp; @H4040@function getValue(){ @H4040@alert("getvaluel"); @H4040@var question =document.insertForm.question.value; @H4040@// alert(question); @H4040@var answer = document.insertForm.answer.value; @H4040@// alert(answer); @H4040@submit(question,answer); @H4040@}; @H4040@function submit(question,answer){ @H4040@xmlHttp=GetXmlHttpObject(); @H4040@if (xmlHttp==null) @H4040@{ @H4040@alert ("Your browser does not support AJAX!"); @H4040@return; @H4040@} @H4040@xmlHttp.onreadystatechange =function(){ @H4040@if(xmlHttp.readyState ==4){ @H4040@alert(xmlHttp.responseText); @H4040@} @H4040@}; @H404_0@var url = "insert1.PHP"; @H_4040@xmlHttp.open("post",url,true); @H4040@xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8"); @H4040@xmlHttp.send("question="+question+"&answer="+answer); } @H4040@function GetXmlHttpObject() @H4040@{ @H4040@var xmlHttp=null; @H4040@try @H4040@{ @H4040@// Firefox,Opera 8.0+,Safari @H4040@xmlHttp=new XMLHttpRequest(); @H4040@} @H4040@catch (e) @H4040@{ @H4040@// Internet Explorer @H4040@try @H4040@{ @H4040@xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); @H4040@} @H4040@catch (e) @H4040@{ @H4040@xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); @H4040@} @H4040@} @H4040@return xmlHttp; @H4040@}@H4040@
@H404_0@然后PHP处理界面,负责跟服务器交换数据 @H_404_0@<div class="codetitle"><a style="CURSOR: pointer" data="45622" class="copybut" id="copybut45622" onclick="doCopy('code45622')"> 代码如下:
<div class="codebody" id="code45622">@H_404_0@<?PHP @H_4040@/* @H4040@* To change this template,choose Tools | Templates @H4040@* and open the template in the editor. @H4040@*/ @H404_0@//echo $POST["question"]; @H404_0@//echo $POST["answer"]; @H404_0@$q =$POST['question']; @H404_0@$a = $POST['answer']; @H4040@//$q='qq'; @H4040@//$a="a"; @H404_0@$con = MysqLconnect("localhost","joe","123"); @H4040@if (!$con) @H4040@{ @H404_0@//die('Could not connect: ' . MysqLerror()); @H404_0@echo 'Could not connect: ' . MysqLerror(); @H4040@} @H404_0@MysqL_selectdb("joe",$con); @H404_0@MysqLquery("INSERT INTO message VALUES ('$q','$a','无')"); @H404_0@MysqLclose($con); @H4040@echo "输入成功"; @H4040@?>@H404_0@

ajaxphp写入数据库

猜你在找的Ajax相关文章