0

我正在使用 ajax 加载一个基于屏幕分辨率显示内容的 php 脚本。该脚本运行良好,但 ajax 调用的 url 也会在脚本中稍后调用——它包含所有处理信息。因此,结果是所有内容都通过 jquery 加载,然后用 php 复制。例子:

<script type="text/javascript">
$(document).ready(function() {
      $.ajax({
         url: 'functions/maxresults.php',
         type: 'GET',
         data: {h: screen.height, w: screen.width}
      }).done(function ( data ) {
          alert("I am working!");
         document.getElementById("container").innerHTML=data;
        });
    });
</script>

再往下

<?php
include_once("functions/maxresults.php");   
?>

<?php
require_once("databasefunctions.php");
?>

<div id="container">
<div id="movieinfo"> 
<?php include("sidebar.php");?>
<?php
// get the function
include_once ('function.php');
if($_GET['page'])
$page = $_GET['page'];
else
$page = 0; 
$maxresults = -1;
if(($_GET['w']) && ($_GET['h'])) {
  $w = $_GET['w'];
  $h = $_GET['h'];
}
  if ($w == 1920) {
    $maxresults = 24;  
  } else if ($w == 1600) {
    $maxresults = 21;  
  } else if ($w == 1440){ 
    $maxresults = 14;
  } else if ($w == 1366) { 
    $maxresults = 21;
  } else if ($w == 1024) {
    $maxresults = 8;
  } else 
    $maxresults = 6;
echo $maxresults;
$currentpage = $page;
$page = $page*$maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM movies WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
$result = GetMoviesByRangeID($page, $maxresults);//Show <maxresults> pages at a time
DisplayResults($result); 
?>
</div>
</div>

我无法删除 php 包含或脚本根本无法工作。在 jquery 中创建一个 include_once 会起作用吗?如果是这样,我该如何编码?

4

1 回答 1

0

PHP 是一个预处理器。因此,将其“稍后”包含在文件中并不重要。您的 include_once 将在 JavaScript 执行之前被处理和输出。

我看起来问题是您的 include_once 脚本也在输出容器 div。因此,当您删除该行时,它会破坏 javascript。试试这个:

  1. 从您的 maxresults.php 文件中删除容器 div。
  2. <?php include_once('functions/maxresults.php);>用一个空的 div替换id 的容器:<div id="container"></div>

你的 JavaScript 现在应该可以工作了:

<script type="text/javascript">
$(document).ready(function() {
  $.ajax({
     url: 'functions/maxresults.php',
     type: 'GET',
     data: {h: screen.height, w: screen.width}
  }).done(function(resp) {
      alert("I am working!");
      $("#container").html(resp);
    });
});
</script>

再往下

<div =id="container"></div>

最大结果.php

<?php
require_once("databasefunctions.php");
?>

<div id="movieinfo"> 
<?php include("sidebar.php");?>
<?php
// get the function
include_once ('function.php');
if($_GET['page'])
$page = $_GET['page'];
else
$page = 0; 
$maxresults = -1;
if(($_GET['w']) && ($_GET['h'])) {
  $w = $_GET['w'];
  $h = $_GET['h'];
}
  if ($w == 1920) {
    $maxresults = 24;  
  } else if ($w == 1600) {
    $maxresults = 21;  
  } else if ($w == 1440){ 
    $maxresults = 14;
  } else if ($w == 1366) { 
    $maxresults = 21;
  } else if ($w == 1024) {
    $maxresults = 8;
  } else 
    $maxresults = 6;
echo $maxresults;
$currentpage = $page;
$page = $page*$maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM movies WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
$result = GetMoviesByRangeID($page, $maxresults);//Show <maxresults> pages at a time
DisplayResults($result); 
?>
</div>
于 2013-03-02T17:05:54.970 回答