Ajax interacts with PHP via POST

a code

conn.php
<?php
     $conn=mysql_connect("localhost","root","root") or die("Database connection failed".mysql_error());
     mysql_select_db("db_database27",$conn) or die("Database connection failed".mysql_error());
     mysql_query("set names gb2312");
?>
 
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Interact with PHP via POST</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 00px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style></head>
<script>
var xmlHttp; //Define the XMLHttpRequest object
function createXmlHttpRequestObject(){
		if(window.ActiveXObject){ //If running under internet explorer
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				xmlHttp=false;
			}
		}else{
			try{ //if running under Mozilla or other browsers
				xmlHttp=new XMLHttpRequest();
			}catch(e){
				xmlHttp=false;
			}
		}
		if(!xmlHttp) //return the created object or display an error message
			alert("Return created object or display error message");
		else
			return xmlHttp;
}
function showsimple(){ //Create the main control function
	createXmlHttpRequestObject();
	var us = document.getElementById("user").value; //Get the value submitted by the form
	var nu = document.getElementById("number").value;
	var ex = document.getElementById("explains").value;
	if(us=="" && nu=="" && ex==""){ //Judging that the value submitted by the form cannot be empty
		alert('Added data cannot be empty!');
		return false;
	}
	var post_method="users="+us+"&numbers="+nu+"&explaines="+ex;		//构造URL参数
	xmlHttp.open("POST","searchrst.php",true); //Call the specified add file
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); //Set the request header information
	xmlHttp.onreadystatechange=StatHandler; //Determine the status value of the URL call and process it
	xmlHttp.send(post_method); //Send data to server
}
function StatHandler(){ //Define the handler function
	if(xmlHttp.readyState==4 && xmlHttp.status==200){ //Judging if the execution is successful, output the following content
		if(xmlHttp.responseText!=""){
			alert("Data added successfully!");
			//Define the data returned by the server into DIV
			document.getElementById("webpage").innerHTML=xmlHttp.responseText;	
		}else{
			alert("Add failed!"); //If the return value is empty
		}
	}
}
</script>
<body>
<table width="800" height="632" border="0" align="center" cellpadding="0" cellspacing="0" background="images/bj.jpg">
  <tr>
    <td width="260" height="245"> </td>
    <td colspan="2" align="center" valign="bottom"><strong>Query employee information, based on employee skill information</strong></td>
    <td width="40"> </td>
  </tr><form id="searchform" name="searchform" method="post" action="#">
  <tr>
    <td height="25"> </td>
    <td width="150" align="right">员工姓名:      </td>
    <td width="350" align="left"><input name="user" type="text" id="user" size="30" /></td>
    <td> </td>
  </tr>
  <tr>
    <td height="25"> </td>
    <td align="right">Employee Number: </td>
    <td align="left"><input name="number" type="text" id="number" size="20" /></td>
    <td> </td>
  </tr>
  <tr>
    <td height="25"> </td>
    <td align="right">Skill Description: </td>
    <td align="left"><textarea name="explains" cols="40" rows="3" id="explains"></textarea></td>
    <td> </td>
  </tr>
  <tr>
    <td height="25"> </td>
    <td colspan="2" align="center">
    <input type="button" name="Submit" value="提交" onclick="showsimple();" />  
    <input type="reset" name="Submit2" value="重置" /></td>
    <td> </td>
  </tr>  </form>
  <tr>
    <td height="268"> </td>
    <td colspan="2" align="center" valign="top"><div id="webpage"></div></td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="2"> </td>
    <td> </td>
  </tr>
</table>
</body>
</html>
 
searchrst.php
<?php
	header('Content-type: text/html;charset=GB2312'); //Specify the encoding format of the sent data
	include_once 'conn/conn.php'; //Connect to the database
	$user =iconv('UTF-8','gb2312',$_POST['users']);				//获取Ajax传递的值,并实现字符编码转换
	$number = iconv('UTF-8','gb2312',$_POST['numbers']);		//获取Ajax传递的值,并实现字符编码转换
	$explains = iconv('UTF-8','gb2312',$_POST['explaines']);	//获取Ajax传递的值,并实现字符编码转换	
	$sql="insert into tb_administrator(user,number,explains)values('$user','$number','$explains')";
	$result=mysql_query($sql,$conn);						//执行添加语句
	if($result){
		$sqles="select * from tb_administrator ";
		$results=mysql_query($sqles,$conn);
		echo "<table width='500' border='1' cellpadding='1' cellspacing='1' bordercolor='#FFFFCC' bgcolor='#666666'>";
		echo "<tr><td height='30' align='center' bgcolor='#FFFFFF'>ID</td><td align='center' bgcolor='#FFFFFF'>名称</td><td align='center' bgcolor='#FFFFFF'>编号</td><td align='center' bgcolor='#FFFFFF'>描述</td></tr>";
		while($myrow=mysql_fetch_array($results)){ 			//循环输出查询结果
 			echo "<tr><td height='22' bgcolor='#FFFFFF'>".$myrow[id]."</td>";
  			echo "<td bgcolor='#FFFFFF'>".$myrow[user]."</td>";
			echo "<td bgcolor='#FFFFFF'>".$myrow[number]."</td>";
			echo "<td bgcolor='#FFFFFF'>".$myrow[explains]."</td>";
			echo "</tr>";
		}
		echo "</table>";  
	}	
?>
 
二 运行结果

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611522&siteId=291194637