包含基于使用Ajax从SQL数据库检索的URL的PHP 页面

问题描述 投票:0回答:1

我有一个页面,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>s(我实际上没有尝试过这个,因为我无法弄清楚我必须把它放在哪里才能使它工作。)

我尝试了多个URL并检查了每个URL是否正确。

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

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

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

php ajax
1个回答
0
投票

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

  1. 如果PHP文件始终位于同一服务器上并且是Web应用程序的一部分,则只需包含它即可。你必须做一些检查和验证,以确保文件在那里等。
  2. 如果网址指向互联网上的任何位置,请将其插入iframe。

解决方案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>
© www.soinside.com 2019 - 2024. All rights reserved.