当谈到 AJAX 时,我是一个完全的菜鸟,只是想知道是否:
创建 ajax 调用时:
$.ajax( {
type: 'POST',
url:'http://link.to.php/file.php',
data: { 'link': variable},
})
我是否必须创建多个 PHP 文件,每个文件只有我想使用的单个查询,或者我可以在一个文件中编译它们吗?例如
**File1.php
//containing a singular query**
<?php
include ('connection.php');
if(isSet($_POST['link'])){
$curUrl=$_POST['link'];
$curUrl=mysql_real_escape_string($curUrl);
$nextSet = "SELECT * FROM shortlink_analytics WHERE shortlink = '$curUrl' ORDER BY hitTime ASC";
$array = array();
$query = mysql_query($nextSet);
while($row = mysql_fetch_array($query)){
$array[] = '<tr><td>'.$row['hitTime'].'</td></tr>';
}
echo json_encode ($array);
}
?>
或者我可以让他们像下面这样:
File2.php
//containing multiple querys
<?php
include ('connection.php');
if(isSet($_POST['link'])){
$curUrl=$_POST['link'];
$curUrl=mysql_real_escape_string($curUrl);
$nextSet = "SELECT * FROM shortlink_analytics WHERE shortlink = '$curUrl' ORDER BY hitTime ASC";
$array = array();
$query = mysql_query($nextSet);
while($row = mysql_fetch_array($query)){
$array[] = '<tr><td>'.$row['hitTime'].'</td></tr>';
}
echo json_encode ($array);
}
if(isSet($_POST['link2'])){
$curUrl2=$_POST['link2'];
$curUrl=mysql_real_escape_string($curUrl2);
$nextSet = "SELECT * FROM shortlink_analytics WHERE shortlink = '$curUr2l' ORDER BY hitTime ASC";
$array2 = array();
$query = mysql_query($nextSet);
while($row = mysql_fetch_array($query)){
$array[] = '<tr><td>'.$row['hitTime2'].'</td></tr>';
}
echo json_encode ($array2);
}
?>
如果我可以像File2.php一样拥有它,我该如何定位正确的查询?