javascript include_once- javascript php调用相同的文件复制内容

时间:2013-03-02 16:48:20

标签: php javascript jquery ajax

我正在使用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 include或脚本根本不起作用。是否会在jquery工作中创建include_once,如果是,我该如何编写它?

1 个答案:

答案 0 :(得分:0)

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

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

  1. 从您的maxresults.php文件中删除容器div。
  2. <?php include_once('functions/maxresults.php);>替换为ID为容器的空div:<div id="container"></div>
  3. 您的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>
    

    <强> maxresults.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>