我在使用 JQuery block 从 MySQL 数据库加载一些数据时遇到问题。
JQuery 代码正在调用位于 public_html 文件夹之外的 PHP 文件(我猜这是问题所在,但无法修复!)。
网站结构:
- public_HTML < Where my domain is pointing
- /index.php
- resources
- /config.php
- /functions
- /fetch-videos.php
- /functions.php
JQuery 位于index.php 中并正在调用fetch-videos.php。
我尝试将 fetch-videos.php 移动到 public_html 文件夹,因为我认为这是因为 Javascript 无法看到该文件,但这不起作用。
值得注意的是,这一切都在我的 WAMP 环境中完美运行,因此我认为这是我在线构建网站的方式的访问问题。
我想尝试保护我的 PHP 文件,因为我被告知这是明智的,但不知道如何从 JQuery 调用它们并实现这一点。
JQuery:
<script type="text/javascript">
$(document).ready(function() {
var track_click = 0;
var total_pages = <?php echo $total_pages; ?>;
var fetch_path = "resources/functions/fetch_videos.php";
$('#results').load(fetch_path, {'page':track_click, 'source': 'index'}, function() {track_click++;});
$(".btn-load-more").click(function (e) {
$(this).hide();
$('.animation_image').show();
if(track_click <= total_pages)
{
$.post(fetch_path,{'page': track_click, 'source': 'index'}, function(data) {
$(".btn-load-more").show();
$("#results").append(data);
$("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500);
$('.animation_image').hide();
track_click++;
}).fail(function(xhr, ajaxOptions, thrownError) {
alert(thrownError); //alert any HTTP error
$(".btn-load-more").show(); //bring back load more button
$('.animation_image').hide(); //hide loading image once data is received
});
}
});
});
fetch-videos.php
<?php
require_once("../config.php");
require_once("../sql_statements.php");
require_once(FUNCTIONS_PATH . "/functions.php");
$item_per_page = 10;
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$source = $_POST["source"];
if($source == 'category'){
$category = $_POST["category"];
$item_per_page = 15;
}
//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
if($source == 'index'){
$rows = db_select(SELECT_LATEST_INDEX . " LIMIT $position, $item_per_page");
}
if($source == 'category'){
$query = str_replace("<<category>>",$category,SELECT_LATEST_CATEGORY);
$rows = db_select($query . " LIMIT $position, $item_per_page");
}
//output results from database
?>
<?PHP
$i = 1;
if($page_number > 0){
}
foreach ($rows as $row){
$video_id = $row['vID'];
$video_title = $row['Title'];
$video_Source = $row['Source'];
$video_SourceID = $row['SourceID'];
$video_uploader = $row['User'];
$video_score = $row['Likes'];
$video_category = $row['Catgeory'];
include(TEMPLATES_PATH . "/video-thumb.php");
$i++;
}
?>
任何帮助将不胜感激,因为我没有主意了!
非常感谢!
<小时/>更新:
我听取了许多人的建议,放弃了保护我的 PHP 文件的尝试,并按如下方式重组了我的网站:
- public_HTML < Where my domain is pointing
- /resources
- /config.php
- /functions
- /fetch-videos.php
- /functions.php
- /index.php
现在客户端可以访问所有内容,我重新运行并在 Chrome 开发者工具的控制台中看到以下错误:
jquery-1.11.1.min.js:4 POST http://www.*********.com/resources/functions/fetch_videos.php 500 (Internal Server Error)
send @ jquery-1.11.1.min.js:4
ajax @ jquery-1.11.1.min.js:4
m.fn.load @ jquery-1.11.1.min.js:4
(anonymous) @ (index):257
j @ jquery-1.11.1.min.js:2
fireWith @ jquery-1.11.1.min.js:2
ready @ jquery-1.11.1.min.js:2
J @ jquery-1.11.1.min.js:2
我已经更新了上面的代码以反射(reflect)新路径。
谢谢
最佳答案
您必须将fetch_videos.php
放入WEB服务器的公共(public)文件夹中。尝试直接在浏览器中加载文件。
请注意,fetch_path
使用 ..
,这是不推荐的。请改用完整路径。
var fetch_path = "/resources/functions/fetch_videos.php";
如果不起作用,请尝试使用Chrome浏览器的调试器(通常是CTRL+SHIFT+I打开)并在“网络”中检查结果。如有必要,按 XHR
(Ajax 查询)过滤。您应该获取缺失的信息来解决您的问题。
关于javascript - JQuery无法调用隐藏目录中的PHP文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45445439/