[英]Send AJAX get request
I just started to study AJAX and I already have some trouble. 我刚开始学习AJAX,已经遇到了一些麻烦。
I created database and table users
with id, name, email
fields. 我创建了具有id, name, email
字段的数据库和表users
。
Then I created php file db.php
for connection with the database 然后我创建了php文件db.php
来与数据库连接
<?php
$dblocation = "localhost";
$dbname = "test5";
$dbuser = "root";
$dbpasswd = "";
$dbcnx = @mysql_connect($dblocation, $dbuser, $dbpasswd);
if (!$dbcnx) {
exit ( "<p>Error.</p>" );
}
if ( ! @mysql_select_db($dbname, $dbcnx)) {
exit ( "<p>Error.</p>" );
}
?>
,created file get-user.php
to return an answer for ajax-request ,创建文件get-user.php
返回ajax请求的答案
<?php
include ("db.php");
$result = mysql_query("SELECT name, email FROM users WHERE id='$id'", $dbcnx);
return $result;
?>
And this is my index.php
file 这是我的index.php
文件
<!DOCTYPE html>
<html>
<head>
<title>index</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#button-1').on('click', function () {
var id = $('#user-id').val();
$.ajax({
type: 'GET',
url: 'get-user.php',
datatype: "html",
data: id,
success: function (data) {
alert(data);
}
});
});
});
</script>
</head>
<body>
<h2>Title</h2>
<input name="id" id="user-id" type="text" size="15" />
<button id="button-1">Get data</button>
</html>
I would like to input the number, click button and get information about user from database, but I get only empty alert. 我想输入数字,单击按钮并从数据库中获取有关用户的信息,但是我只会收到空警报。 How to make it work? 如何使其运作?
Sorry for asking, I'm just newbie. 抱歉问,我只是新手。
These changes are require: 这些更改是必需的:
Ajax: data: id,
to data: "{id : id},
Ajax: data: id,
至data: "{id : id},
PHP: return $result;
PHP: return $result;
to echo $result;
echo $result;
PHP & MySQL: PHP和MySQL:
$result = mysql_query("SELECT name, email FROM users WHERE id='$id'", $dbcnx);
$row = mysql_fetch_array($result);
return $result[0] . ";" . $row[1];
The correct way to pass the parameters is this 传递参数的正确方法是这样
$.ajax({
type: 'GET',
url: 'get-user.php',
datatype: "html",
data: { id: id },
success: function (data) {
alert(data);
}
});
In the php
return the value like this 在php
返回这样的值
<?php
include ("db.php");
$result = mysql_query("SELECT name, email FROM users WHERE id='$id'", $dbcnx);
$fetch=mysql_fetch_array($result);
return $fetch["email"];
?>
Try this, 尝试这个,
Javascript: Javascript:
$.ajax({
type: 'GET',
url: 'get-user.php',
datatype: "html",
data: {id:id},
success: function (data) {
alert(data);
}
});
in get-user.php: 在get-user.php中:
<?php
if(isset($_GET['id'])){
include ("db.php");
$id = $_GET['id'];
....
}
?>
Note: Use mysqli_* functions or PDO instead of mysql_* functions(deprecated) 注意:使用mysqli_ *函数或PDO代替mysql_ *函数(已弃用)
You are very close, but there are a few things you need to change. 您非常亲密,但是需要更改一些内容。
first inside of your ajax call: 首先在您的ajax调用中:
$.ajax({
type: 'GET',
url: 'get-user.php',
datatype: "html",
data: {'id': id},
success: function (data) {
alert(data);
}
});
notice the change in the data
option. 注意data
选项中的更改。
Second, inside your get-user.php
you refer to the value using $_GET['id']
. 其次,在您的get-user.php
中,使用$_GET['id']
引用值。
Finally, instead of return
you want to echo
the response. 最后,您要echo
显响应而不是return
。
Ajax code Ajax代码
data: {id: id};
php code PHP代码
use pdo instead of mysql_query, the code you use on mysql_query
its not prevent sql injection. 使用pdo代替mysql_query,在mysql_query
使用的代码不会阻止sql注入。
include ("db.php");
$id = $_GET['id'];
$stmt = $pdo->prepare('SELECT name, email FROM users WHERE id = :id');
$stmt->execute(array('id' => $id));
$result = $stmt->get_result();
echo $result;
$stmt = $dbConnection->prepare('SELECT name, email FROM users WHERE id = ?');
$stmt->bind_param('id', $id);
$stmt->execute();
$result = $stmt->get_result();
echo $result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.