<html> <head> </head> <body> <form action="7.php"> 地址:<input type="text" name="addr" value="localhost"> 用户:<input type="text" name="name" value="root"> 密码:<input type="text" name="pass" value="root"> <input type="button" value="提交" onclick="a();"> </form> <script src="js\jquery-1.11.2.min.js"></script> <script> function a(){ var cont=$("input").serialize(); $.ajax({ url:"7.php", type:‘post‘, data:cont, success:function (data){ var res = eval("(" + data + ")");//转为Object对象 //用JQ来取值 // $.each(res,function(i,v){ // $.each(v,function(m,n){ // document.write(m+"-----"+n+"<br>"); // }) // }) //用for来取值 for (var i in res) { for (var n in res[i]){ document.write(n+"-----"+res[i][n]+"<br>"); } } // document.write("Host:"+res[0]["Host"]+"<br>"); // document.write("User:"+res[0]["User"]+"<br>"); // document.write("Pass:"+res[0]["Password"]+"<br>"); } }) } </script> </body> </html>
PHP页面
<?php $name=$_POST["name"]; $pass=$_POST["pass"]; $addr=$_POST["addr"]; $conn=mysql_connect($addr,$name,$pass); if (!$conn) { die(‘Could not connect: ‘ . mysql_error()); } mysql_select_db("mysql"); $str="select * from user;"; $result=mysql_query($str); for ($i=0; $arr[$i]=mysql_fetch_assoc($result) ; $i++) ; array_pop($arr); $arr=json_encode($arr); echo $arr; mysql_close($conn); ?>
原文:http://sr0517.blog.51cto.com/809286/1845625