首页 > 解决方案 > 在 php 中回显时 Javascript 不起作用

问题描述

我正在尝试为网站上的用户创建文件共享页面。根据他们的角色,他们可以看到不同的文件夹。当我用 html 编写所有内容时,它运行良好,每个文件夹的侧面都有三个按钮,但我的 javascript 函数隐藏了未点击的按钮。当我将所有内容放入 php 中时,基本上只是在回显它,这一切都停止了工作,并且显示了所有文件夹。我需要把它放在 php 中,因为我需要为不同的用户显示不同的东西。这是代码:

echo "
<!DOCTYPE html>
<html>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' href='../styles/style.css'>
<body>
<header>
<h1>Dundas Agri Systems Employee Page</h1>
<h2>Always search for a Spirit of Tranquility</h2>
</header>";


echo "<div class='tab'>";
if ($_SESSION["role"]==admin || $_SESSION["role"]==tech || $_SESSION["role"]==emp){
echo "<button class=''tablinks'' onclick=''OpenFolder(event, 'Employee')'' id=''defaultOpen''>Employee</button>";
}
if ($_SESSION["role"]==admin || $_SESSION["role"]==tech){
echo "<button class=''tablinks' onclick=''openFolder(event, 'Lely')''>Lely</button>";
}
if ($_SESSION["role"]==admin){
echo "<button class='tablinks' onclick='openFolder(event, 'Manager')''>Manager</button>";
}
echo "</div>";
if ($_SESSION["role"]==admin || $_SESSION["role"]==tech){
echo "<div id='Lely' class='tabcontent'>
<div id='container'>
<h1>Directory Contents</h1>

<table class='sortable'>
  <thead>
    <tr>
      <th>Filename</th>
      <th>Type</th>
      <th>Size <small>(bytes)</small></th>
      <th>Date Modified</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
";      
openfilesystem('./LelyUploads');   
echo "   
</tbody>
</table>

<div id='Upload'>
    <form action='' method ='post' enctype='multipart/form-data'>
        Select a file to upload:
        <input type='file' name='fileToUpload' id='fileToUpload'>
        <input type='submit' value='Submit' name='submit' id='submit'>
    </form>
</div>

</div>
</div>";
}
if ($_SESSION["role"]==admin || $_SESSION["role"]==tech || $_SESSION["role"]==emp){   
echo "<div id='Employee' class='tabcontent'>
<div id='container'>

<h1>Directory Contents</h1>

<table class='sortable'>
  <thead>
    <tr>
      <th>Filename</th>
      <th>Type</th>
      <th>Size <small>(bytes)</small></th>
      <th>Date Modified</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
";
openfilesystem('./uploads');    
echo "
</tbody>
</table>

<div id='Upload'>
    <form action='' method ='post' enctype='multipart/form-data'>
        Select a file to upload:
        <input type='file' name='fileToUpload' id='fileToUpload'>
        <input type='submit' value='Submit' name='submit' id='submit'>
    </form>
</div>

</div>
</div>";
}
if ($_SESSION["role"]==admin){  
echo "<div id='Manager' class='tabcontent'>
<div id='container'>

<h1>Directory Contents</h1>

<table class='sortable'>
  <thead>
    <tr>
      <th>Filename</th>
      <th>Type</th>
      <th>Size <small>(bytes)</small></th>
      <th>Date Modified</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
";
    openfilesystem('./LelyUploads');

echo "
</tbody>
</table>

<div id='Upload'>
    <form action='' method ='post' enctype='multipart/form-data'>
        Select a file to upload:
        <input type='file' name='fileToUpload' id='fileToUpload'>
        <input type='submit' value='Submit' name='submit' id='submit'>
    </form>
</div>

</div>
</div>";
}


echo "
<script>
function openFolder(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName('tabcontent');
for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = 'none';
}
tablinks = document.getElementsByClassName('tablinks');
for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(' active', '');
}
document.getElementById(cityName).style.display = 'block';
evt.currentTarget.className += '' active';
}

// Get the element with id='defaultOpen' and click on it
// document.getElementById('defaultOpen').click();
</script>

</body>
</html>
";

关键是函数 OpenFolder 似乎没有被正确调用,或者根本没有被调用。无论如何,我都不是 PHP 专家,所以如果有另一种方法来完成我正在尝试做的事情,那也会有很大的帮助!

标签: javascriptphpecho

解决方案


推荐阅读