2017-12-18 105 views
0

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

但是,我不希望显示网址,而是希望页面的内容例如a1701.php将以与使用<?php include 'a1701.php' ?>时相同的方式显示。

我已经阅读了很多关于SO帖子,但没有发现任何描述这种情况下(也许我找错了东西在这种情况下,请指教)。继其他部分相关职位的建议,我已经试过几件事包括:

  • 使用绝对而不是相对链接与$_SERVER['DOCUMENT_ROOT']
  • include 'a1701.php'; VS echo "<?php include 'a1701.php'; ?>"
  • 使用&lt;代替<
  • 重装具体<div> S(我还没有真正尝试这一点,因为我想不通,我将不得不放在哪里,使其工作什么代码。)

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

index.php 

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

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

filter.php 

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

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

 
if($where != NULL) { 
 
\t $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 

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

有没有办法实现我所追求的?我正朝着正确的方向前进吗?

+0

如果我理解你正确,你想要a1701.php执行并输出一些可以插入div的html。是对的吗? – ryantxr

+0

@ryantrx,这是正确的。 –

+0

@ryantrx,我可能误解了你的要求,所以澄清: 'a1701.php'不是发送Ajax请求的地方。 'a1701.php'只是我想包含在'index.php'中的内容(数据库中的一个字段为每个记录返回一个url,这个特定记录的url是'a1701.php')。目前,网址显示正常,但我想交换此网址的文件的实际内容 - 这一点是我无法工作的部分。 –

回答

0

我可以想出2种方法来实现这一点。

  1. 如果PHP文件始终在同一台服务器上,并且是Web应用程序的一部分,只是包含它。你将不得不做一些检查和验证,以确保该文件是存在的等

  2. 如果URL指向互联网上的任何地方,将它插入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 src="<?=$row['url']?>"></iframe> 
+0

谢谢@ryantxr,它现在正在工作。我不知道为什么它不是之前,但我使用你发布的内容(选项1)摆弄它,现在它可以工作。 –