我正在尝试创建PHP函数,我将能够在javascript代码中使用。
<!DOCTYPE html>
<?php
include 'pdo_connect.php';
function pleaseWork($query) {
return dataQuery($query)->fetchAll();
}
function makeQuery($query)
{
$work = pleaseWork($query);
$json = json_encode($work);
}
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<title>Admin check</title>
<meta charset="UTF-8">
</head>
<body>
<script type='text/javascript'>
<?php makeQuery("SELECT * FROM `grupy`");?>
var jArray = <?= $json ?>;
console.log(jArray);
</script>
当然,它不起作用,因为JS块中的代码不知道从文件开头开始的任何变量。我该怎么做?我从来没有使用过AJAX之类的东西,所以我不知道该怎么做。
发布于 2016-02-17 05:23:39
您需要更详细地学习OOP (面向对象编程)。也许可以搜索依赖注入,或者异常。此外,还需要创建一个PHP脚本,该脚本接受ajax请求并调用必要的函数或方法,这些函数或方法都应该分离到各自的文件和类中。这将使不同的数据层彼此分离(即表示、业务逻辑、数据)。你也应该花一些时间来学习MVC(模型视图控制器)是关于什么的。这将帮助您理解分离的重要性。
现在,我将向您展示问题的快速解决方案。假设您的文件结构都在相同的目录中:
|--- ajax_requests.php
|--- Database.php
|--- Functions.php
|--- index.php
index.php是您的 /jQuery所在的位置:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<title>Admin check</title>
<meta charset="UTF-8">
</head>
<body>
<script type='text/javascript'>
$.ajax({
type: "post",
url: "ajax_requests.php",
data: {request: "get_grupy_json"},
success: function(result){
console.log(result);
}
});
</script>
注意我们是如何使用jQuery发出Ajax请求的。我们向文件ajax_requests.php
发出post
请求,将get_grupy_json
作为我们的request
参数发送。您的前端视图上不应存在任何SQL查询。
ajax_requests.php接收请求,获取数据库对象并将其发送到Functions对象,然后检查请求是否作为Functions类的方法存在,如果存在,则运行该方法并将结果转换为json (请记住自己添加错误检查):
<?php
if (!empty($_POST)) {
$method = $_POST['request'];
include 'Database.php';
include "Functions.php";
$db = new Database();
$functions = new Functions($db);
if (method_exists($functions, $method)) {
$data = $functions->$method();
header('Content-Type: application/json');
echo json_encode($data);
}
}
Functions.php
class Functions
{
private $db;
public function __construct(Database $db)
{
$this->db = $db;
}
public function get_grupy_json()
{
$query = "SELECT * FROM `grupy`";
$result = $this->db->dataQuery($query);
return $result->fetchAll();
}
}
Database.php
class Database
{
private $conn = null;
public function __construct()
{
try {
$username = "root";
$password = "root";
$servername = "localhost";
$dbname = "test";
$this->conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
trigger_error("Error: " . $e->getMessage());
}
}
public function dataQuery($query, $params = array())
{
try {
$stmt = $this->conn->prepare($query);
$stmt->execute($params);
return $stmt;
} catch (PDOException $e) {
trigger_error("Error: " . $e->getMessage());
};
}
}
这是我所做的事情的一个粗略的模型。我希望你能理解这一切是如何分离的,这样你就可以在应用程序增长的时候轻松地向它添加特性。
发布于 2016-02-17 02:31:50
AJAX不是这样工作的。基本上,PHP的产品是一个HTML页面(使用JS),在浏览器中根据请求呈现。当用户执行某个操作时,浏览器可能会向不同的PHP脚本发出另一个请求,从而显示AJAX检索到的数据。在执行第一个请求时,此请求的结果不会显示给用户,而是传递给当前显示页面的JS中的函数(这就是AJAX的原因)。然后,此函数可以以任何它想要的方式处理数据。
看看这个:http://www.w3schools.com/jquery/jquery_ajax_intro.asp。
https://stackoverflow.com/questions/35440184
复制相似问题