Ajax如何调用后台php代码访问数据库
时间:2011-10-18
来源:互联网
Ajax如何调用数据库,这里我用的JS代码是function check{
var xmlHttp
function showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser.php" //getuser.php是我的PHP文件,功能是链接数据库并且访问
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}
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
//连接数据库的函数
$con = mysql_connect('localhost', 'root', '123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("storage", $con);
$sql="SELECT * FROM user_main";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['text'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
html代码是:<html>
<head>
<script src="selectuser.js"></script>
</head>
<body>
<form method="post" onSubmit="return showUser();">
Select a User:
<input type="submit" name="users" value="提交" >
</form>
<p>
<div><b>User info will be listed here.</b></div>
</p>
</body>
</html>
求各位大侠指教
var xmlHttp
function showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser.php" //getuser.php是我的PHP文件,功能是链接数据库并且访问
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}
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
//连接数据库的函数
$con = mysql_connect('localhost', 'root', '123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("storage", $con);
$sql="SELECT * FROM user_main";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['text'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
html代码是:<html>
<head>
<script src="selectuser.js"></script>
</head>
<body>
<form method="post" onSubmit="return showUser();">
Select a User:
<input type="submit" name="users" value="提交" >
</form>
<p>
<div><b>User info will be listed here.</b></div>
</p>
</body>
</html>
求各位大侠指教
作者: wshiwushengzhong 发布时间: 2011-10-18
急。。。!求大侠帮帮我,我会后续加分的
作者: wshiwushengzhong 发布时间: 2011-10-18
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28