包含基于使用Ajax从

时间:2017-12-18 02:21:55

标签: php ajax

我有一个index.php页面,<select> <options>用作过滤器。通过Ajax,从SQL数据库中检索信息并在同一页面上回显到<div>。其中一个回显的字段包含另一个页面的URL,例如a1701.php。到目前为止,一切都很完美。

但是,我想要显示网页的内容,而不是显示网址。 a1701.php的显示方式与我使用<?php include 'a1701.php' ?>时的显示方式相同。

我已经在SO上阅读了很多帖子,但没有找到任何描述这种情况(也许我正在寻找错误的东西,在这种情况下请告知)。根据其他部分相关帖子的建议,我尝试了几个方面,包括:

  • 使用$_SERVER['DOCUMENT_ROOT']
  • 的绝对链接而不是相对链接
  • include 'a1701.php'; vs echo "<?php include 'a1701.php'; ?>"
  • 使用&lt;代替<
  • 重新加载特定的<div>(我实际上没有尝试过这个,因为我无法弄清楚我必须在哪里设置代码才能使其正常工作。)

我尝试了多个网址并检查过每个网址是否正确。

index.php

<script>
    function filterQuestions() {
    	var selectCount = document.getElementsByTagName("select").length;
    	var str = [];
    	for (var i = 0; i < selectCount; i++) {
    		if (document.getElementsByTagName("select")[i].value != "") {
    		str[i] = document.getElementsByTagName("select")[i].name+"="+document.getElementsByTagName("select")[i].value;    			
    		}
    	}
  		if (window.XMLHttpRequest) {
  			xmlhttp = new XMLHttpRequest();    			
  		} else {
  			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		}
  		xmlhttp.onreadystatechange = function() {
  			if (this.readyState == 4 && this.status == 200) {
  				document.getElementById("questionList").innerHTML = this.responseText;
  			}
  		};
  		xmlhttp.open("GET","filter.php?"+str.join("&"),true);
  		xmlhttp.send();
    }
    </script>
    
    

		<select name="branch" onchange="filterQuestions()">
			<option value="All">All branches</option>
			<option value="Number">Number</option>
			<option value="Trigonometry">Trigonometry</option>
		</select>
		<select name="topic" onchange="filterQuestions()">
			<option value="All">All topics</option>
			<option value="sinrule">Sine Rule</option>
			<option value="cosrule">Cosine Rule</option>
		</select>

filter.php

<?php
$branch = $_GET["branch"];
$topic = $_GET["topic"];

if($branch != "All") {
	$wherefilter[] = "branch = '".$branch."'";
}
if($topic != "All") {
	$wherefilter[] = "topic = '".$topic."'";
}
$where = join(" AND ", $wherefilter);

if($where != NULL) {
	$where = " WHERE $where";
}

mysqli_select_db($link,"generator");
$sql="SELECT question_name, url FROM questions".$where;
$result = mysqli_query($link,$sql);
echo "<table>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['question_name'] . "</td>";
    echo "<td>" . $row['url'] . "</td>";
    echo "</tr>";
    $pagelink = $row['url'] . '.php'; /* URL is correct */
    echo"<br>";
    echo $pagelink;
    echo"<br>";
    echo "<?php include '" . $pagelink . "'; ?>";
    echo "<br>";
    echo "<?php include '" . $pagelink . "'.php; ?>"; /* doesn't work */
    include $pagelink; /* doesn't work */
}
echo "</table>";
mysqli_close($link);
?>

a1701.php

包含我想要包含的内容。我也尝试过包含其他内容。

有没有办法实现我的目标?我正朝着正确的方向前进吗?

1 个答案:

答案 0 :(得分:0)

我可以想到两种方法来实现这一目标。

  1. 如果PHP文件始终位于同一台服务器上并且是Web应用程序的一部分,请将其包含在内。您必须进行一些检查和验证,以确保文件存在等。

  2. 如果网址指向互联网上的任何位置,请将其作为iframe插入。

  3. 解决方案1(一切都是本地的)

    假设有一些名为getPhpFileName的函数返回PHP文件的名称。你需要php文件的实际名称,而不是指向它的url。该文件直接从文件系统读取,而不是通过Web服务器读取。

    $phpFile = getPhpFileName($row['url']);
    if ( file_exists($phpFile) ) {
        @include $phpFile;
    }
    

    example1.php(这是要包含的文件)

    <div>Hello Example1</div>
    

    解决方案2(iframe) 在这种情况下,返回iframe,浏览器将负责从URL获取输出并将其插入页面。

    <iframe src="<?=$row['url']?>"></iframe>