AJAX使用php和javascript连接到db
AJAX connecting to db with php and javascript
我以前从未使用过ajax,并且正在尝试完全理解这个类似于我想做的事情的例子。 以下是我关于示例代码的问题。
-
我是否需要包含某种 ajax 标头脚本才能使用它???
-
这部分在 html 文件中做什么:
-
xmlhttp.open("GET","getuser.php?q="+str,true);
-
xmlhttp.send();
-
-
这是我遇到最大问题的地方。 什么是q? 是 vaule ex:1、2、3 等吗??
-
$q=$_GET["q"];
-
这是 html 文件....
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
这是名为getuser.php的php文件...
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
- 不。它是 EMCAScript 中的标准规范。
- 该特定代码行打开对
getuser.php
文件的GET
请求,将 GET 参数q
作为 JS 中的str
值传递。 - 在该特定实例中,$q被设置为
GET
值的值q
。
简而言之,您的JS调用PHP脚本来执行自身,同时传递一个变量来(表面上)修改脚本的结果。
如果您不了解GET
和POST
变量,我建议您至少阅读 PHP 手册中关于 $_GET
数组的页面,或者对GET
和POST
变量的更一般概述。
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
这将打开文件,然后将 url 发送到 getuser.php。基础知识:P
$q=$_GET["q"];
这是 ajax 文件发送到 getuser.php 文件的内容。这不应该是一个"安全"变量,因为get很容易被黑客入侵。如果需要,请尝试使用帖子。
希望我能帮助:)
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 如何使用密码检测网络中的状态连接
- 如何处理node.js节点mongodb中的连接和查询队列
- Rails-JQueryUIAutcomplete和AJAX不工作,可以't连接到数据库
- Twilio-显示所有连接参与者的远程参与者视频
- 操作放置在画布上的元素之间的连接
- TypeError:_this.store.getState在使用来自Redux的连接时不是函数
- jQuery UI可排序-多连接列表拖动
- 如何使用offer/answer交换来自两个对等连接的流
- 在Qualtrics中,介绍如何动态连接两个滑块
- 使用优化器在慢速连接上加载main.js时需要js超时
- AJAX和phpJSON连接DB和retern值
- 在连接DB - NodeJs的函数中使用app.get
- 连接DB和Ajax(没有php)
- 使用JS和ADO连接到本地DB的各种问题
- 如何在node.js中访问整个应用程序中的db连接
- 带有DB连接的JavaScript / HTML5游戏
- 使用vb脚本连接oracle 12c db时出错
- 在php的db连接函数的错误信息中显示js警告