ajax 实现 GET POST 异步 同步 检验mysql连接情况简单实例

来源:互联网 发布:腾讯棋牌游戏数据分析 编辑:程序博客网 时间:2024/05/17 07:25

test.html


<!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=utf8" /><title>test</title><script language="javascript">function $(id){return document.getElementById(id) ;}function get_get(){var xmlhttp ;var host = $("host").value ;var user = $("user").value ;var password = $("password").value ;if(window.XMLHttpRequest){//for firefox , sarafi , opera  , chrome , IE7+xmlhttp = new XMLHttpRequest() ;}else{//for IE5 , IE6xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") ;}var url = "test.php?host=" + host + "&user=" + user + "&password=" + password ;xmlhttp.open("GET" , url , false) ; //or true/*Async = true modexmlhttp.onreadystatechange = function (){if(xmlhttp.readyState == 4 && xmlhttp.status == 200){$("test").innerHTML = xmlhttp.responseText ;}}*/xmlhttp.send() ;//Async = false mode$("test").innerHTML = xmlhttp.responseText ;}function get_post(){var xmlhttp ;var host = $("host").value ;var user = $("user").value ;var password = $("password").value ;var url = "test.php" ;var send_value = "host=" + host + "&user=" + user + "&password=" + password ;if(window.XMLHttpRequest){//for firefox , IE7+ , sarafi , chrome , operaxmlhttp = new XMLHttpRequest() ;}else{//for IE5 , IE6xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") ;}xmlhttp.open("POST" , url , false ) ;//or true xmlhttp.setRequestHeader("content-type" , "application/x-www-form-urlencoded") ; /*//Async = true modexmlhttp.onreadystatechange = function (){if(xmlhttp.readyState == 4 && xmlhttp.status == 200){$("test").innerHTML = xmlhttp.responseText ;}}*/xmlhttp.send(send_value) ;//Async = false mode$("test").innerHTML = xmlhttp.responseText ;}</script><style type="text/css">#host{border:1px solid #000;margin:0px 0px 0px 70px;}#user{border:1px solid #000;margin:0px 0px 0px 60px;}#password{border:1px solid #000;margin:0px 0px 0px 19px;}#get_test{border:1px solid #000;border:1px solid #0000;cursor:pointer;}</style></head><body><span>Mysql Host:</span><input type="text" name="host" id="host" /><br /><br /><span>MySQL User:</span><input type="text" name="user" id="user" /><br /><br /><span>MySQL Password:</span><input type="password" name="password" id="password" /><br /><br /><span>Test MySQL Connect:</span>  <input type="button" value="test" id="get_test"onclick="get_post()" /><br /><br /><span>Test Result:</span>  <span id="test"></span></body></html>

test.php


<?phperror_reporting(0) ;//test mysql connectfunction test_connect($host , $user , $password){$mysqli = new mysqli($host , $user , $password) ;$html ;if($mysqli->connect_error){$html = "Can not connect to MySQL" ;}else{$html = "Connect successful" ;}return $html ;}$host = $_GET['host'] ;//get mysql host$user = $_GET['user'] ;//get mysql user$password = $_GET['password'] ;//get mysql password$host = $_POST['host'] ;$user = $_POST['user'] ;$password = $_POST['password'] ;$test_html = test_connect($host , $user , $password) ;echo $test_html ;?>


原创粉丝点击