如何在PHP

时间:2018-11-05 16:12:32

标签: javascript php html css

我在href中放置了一个onclick,并且href是PHP中的,因为我想根据用户是否已经登录来更改nav-item。问题是当我单击“登录”时,模态窗口不会弹出。预先感谢。

HTML

<nav class="navbar navbar-inverse">
<ul class="nav navbar-nav">       
<?php
if($_SESSION["username"] == NULL) { 
  echo '<li class="nav-item">
  <a id="myLink" href="#" onclick="document.getElementById(id01).style.display=block" style="width:auto;">Login</a></li>';
    }
    ?>    
  <li class="nav-item"><a href="hci_addEvent.php">Add Event</a></li>     
  <li class="nav-item"><a href="hci_homepage.php">Home</a></li>
</ul>
</nav>

 <div id="id01" class="modal">
<form class="modal-content animate" action="/action_page.php">
<div class="imgcontainer">
  <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
  <img src="img_avatar2.png" alt="Avatar" class="avatar">
</div>
<div class="container">
  <label for="uname"><b>Username</b></label>
  <input type="text" placeholder="Enter Username" name="uname" required>
  <label for="psw"><b>Password</b></label>
  <input type="password" placeholder="Enter Password" name="psw" required>
  <button type="submit">Login</button>
</div>

 

JAVASCRIPT

var modal = document.getElementById('id01');
window.onclick = function(event) {
if (event.target == modal) {
    modal.style.display = "none";
}
}

2 个答案:

答案 0 :(得分:1)

您需要在id01周围加上引号,如下所示:

getElementById(\'id01\')

反斜杠转义了引号,因此不会引起PHP语法错误。

答案 1 :(得分:0)

尝试一下

echo '<li class="nav-item">
  <a id="myLink" href="#" onclick="document.getElementById(\'id01\').style.display=block" style="width:auto;">Login</a></li>';