Ajax,PHP-从数据库获取 [英] Ajax, PHP - fetch from database
问题描述
我在收集从数据库中获取的数据时遇到一些问题.不知道如何继续.
i have some problems with collecting the data i fetch from database. Dont know how to continue.
到目前为止我做了什么:
What i did so far:
JQ:
$(document).ready(function(){
$('#submit').click(function(){
var white = $('#white').val();
$.ajax({
type:"POST",
url:"page.php",
data:{white:white}
});
});
});
到目前为止,
PHP(需要的page.php):
$thing = mysql_real_escape_string($_POST["white"]);
..database connect stuff..
$query = "SELECT * FROM table1 WHERE parameter='$thing'";
if($row = mysql_query($query)) {
while (mysql_fetch_array($row)) {
$data[]=$row['data'];
}
}
我不知道的是如何使用ajax发送和接收数据.
What i dont know, is how to send out data and receive it with ajax.
请求不成功怎么办?
ajax调用对数据库注入的安全性如何?
How secure is ajax call against database injection?
谢谢:)
推荐答案
拨打电话后,您需要在$.ajax()
中使用success
参数来获得响应
You'll need a success
parameter in $.ajax()
to get a response once a call is made
$('#submit').click(function(){
var white = $('#white').val();
if(white == '')
{
// display validation message
}
else
{
$.ajax({
type:"POST",
url:"page.php",
data:{"white":white}
success:function(data){
$('#someID').html(data);
}
});
});
在page.php
中回显的任何内容(HTML标记或变量)将显示在ID为someID
的元素中,最好将其保留为<div>
Whatever you echo (HTML tags or variables) in page.php
will be shown in the element whose ID is someID
, preferable to keep the element a <div>
在page.php
中,您可以使用$_POST['white']
捕获输入元素中输入的值,并将其用于执行您想执行的任何DB操作
In page.php
, you can capture the value entered in the input element by using $_POST['white']
and use it to do whatever DB actions you want to
这篇关于Ajax,PHP-从数据库获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!