【问题标题】:Drop Down Select File And Include It into Same Page下拉选择文件并将其包含在同一页面中
【发布时间】:2014-02-26 19:01:01
【问题描述】:

尝试这样做,但它只是回显值,而不是实际选择要包含到同一页面中的 .php 文件。代码如下...

<body>
<form name="form1" method="POST" action="test.php">
<select name="select" onChange="document.form1.submit()">
 <option selected>Select an Industry</option>
 <option value="members">members</option>
 <option value="Female">Female Members</option>
</select>
</form>

<?php

$file = $_POST['select'];
echo $file;


$path_file = $_SERVER['DOCUMENT_ROOT']/$file.php;
if(file_exists($path_file))
{
  echo("The file does not exist at: $path_file");
}
else
{
  require_once($path_file);
}?>
</body>

一旦排序,我希望它会在同一页面中包含一个 php 文件!在更改之前还需要一个默认页面,所以成员是主页面,然后人们可以选择女性来查看女性等。

【问题讨论】:

  • 这两个代码块是否在同一个页面上?
  • 是的,它通过 echo $file 回显值;所以也需要包含它

标签: php jquery html include


【解决方案1】:

改变

$path_file = $_SERVER['DOCUMENT_ROOT']/$file.php;

$path_file = $_SERVER['DOCUMENT_ROOT']."/".$file.".php";

但这对安全性非常不利(您不能信任 _POST 数据)。最好改为:

if (isset($_POST['select']) && $_POST['select'])
{
if ($_POST['select'] == 'something')
 include 'something.php';
if ($_POST['select'] == 'somethingelse')
 include 'another.php';
} else {
  include 'default.php';
}

【讨论】:

  • 如果 $_POST['select'] 为空?只需添加'else': if (isset($_POST['select']) && $_POST['select']) { .... } else { include 'main.php'; }
  • 我得到了这个伙伴,PHP Parse 错误:语法错误,第 18 行 /home/name/public_html/test.php 中的意外 '}'
猜你喜欢
  • 1970-01-01
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 2021-02-01
相关资源
最近更新 更多