2015-02-17 91 views
0

你好,我对编程一般都很陌生,但我得到了我需要的基础知识。如何从不同的html/php文件中获得document.getElementById()。innerHTML

我有我的index.html包含此:

<!-- Navigation --> 
<nav class="navbar navbar-default navbar-fixed-top"> 
    <div class="container"> 

     <!-- Collect the nav links, forms, and other content for toggling --> 
     <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
      <ul class="nav navbar-nav navbar-right"> 
       <li class="hidden"> 
        <a href="#page-top"></a> 
       </li> 
       <li class="page-scroll"> 
        <a href="#portfolio">Portfolio</a> 
       </li> 
       <li id="navbutone" class="page-scroll"> 
        <a href="login.php">Login</a> 
       </li> 
       <li id="navbuttwo" class="page-scroll"> 
        <a href="register.php">Register</a> 
       </li> 
      </ul> 
     </div> 
     <!-- /.navbar-collapse --> 
    </div> 
    <!-- /.container-fluid --> 
</nav> 

记住我得到这个从我的编辑这样一个网站模板,我没有想出这个布局

和我有当这部分代码在运行中有一些HTML去替代列表的内容的PHP文件:

<?php 
if($login_ok) 
    { 
?> 
<script type="text/javascript"> 
function logedin() { 
    document.getElementById("one").innerHTML = "<a href="logout.php">Logout</a>"; 
} 
</script> 
<script type="text/javascript"> 
    logedin(); 
</script> 
<?php 
header("Location: index.html"); 
     die("Redirecting to: private.php"); 
    } 
?> 

这不工作,我不知道如果这是连关 或不。 在此先感谢您的帮助。 我也可以补充说,他们链接到login.php他们通过在PHP的底部的HTML表单登录。

?> 
<h1>Login</h1> 
<form action="login.php" method="post"> 
Username:<br /> 
<input type="text" name="username" value="<?php echo $submitted_username; ?>" /> 
<br /><br /> 
Password:<br /> 
<input type="password" name="password" value="" /> 
<br /><br /> 
<input type="submit" value="Login" /> 
</form> 
<a href="register.php">Register</a> 

<script src="index.html"></script> 

</html> 

更新:我发现我所需要的,而不是用PHP文件,我只是把我的index.html的是这样的链接就会变出什么搞乱:

<?php 
       require("common.php"); 
       if(empty($_SESSION['user'])) 
        { 
         ?> 
         <li class="page-scroll"> 
          <a href="login.php">Login</a> 
         </li> 

         <li class="page-scroll"> 
          <a href="register.php">Register</a> 
         </li> 
         <?php 
        } 
       else 
        { 
         ?> 
         <li class="page-scroll"> 
          <a href="logout.php">Logout</a> 
         </li> 

         <li class="page-scroll"> 
          <a href="private.php">Members Page</a> 
         </li> 
         <?php 
        } 
       ?> 

中的common.php只是连接到我的数据库。

+1

我认为你是这样做的全部错误。很确定你不能访问浏览器之外的html dom(如果浏览器没有加载它,就不能访问它)。你需要做的是做更多的PHP工作。它看起来像你想要改变一个链接,如果用户登录? – floor 2015-02-17 20:28:56

+0

为什么你的index.html与你的php文件分开? – floor 2015-02-17 20:30:26

+0

您可以发布所有代码,以便完整了解您要在此处执行的操作。 – floor 2015-02-17 20:32:55

回答

0

我明白了。在index.html我只是把

<li class="hidden"> 
    <a href="#page-top"></a> 
</li> 

<li class="page-scroll"> 
    <a href="#portfolio">Portfolio</a> 
</li> 

<?php 
require("common.php"); 
if(empty($_SESSION['user'])) 
    { 
     ?> 
     <li class="page-scroll"> 
      <a href="login.php">Login</a> 
     </li> 

     <li class="page-scroll"> 
      <a href="register.php">Register</a> 
     </li> 
     <?php 
    } 
else 
    { 
     ?> 
     <li class="page-scroll"> 
      <a href="logout.php">Logout</a> 
     </li> 

     <li class="page-scroll"> 
      <a href="private.php">Members Page</a> 
     </li> 
     <?php 
    } 
?> 

正是我所需要的,没有搞乱其他的PHP文件。

0

用途使用"相反,你应该使用'

<script type="text/javascript"> 
function logedin() { 
    document.getElementById("one").innerHTML = "<a href='logout.php'>Logout</a>"; 
} 
</script> 
+0

即使他修复了语法错误,如果它没有加载到dom中,他可以访问该html文件吗? – floor 2015-02-17 20:27:02

+0

这仍然不起作用。 – 2015-02-17 20:27:55

+0

这取决于... – void 2015-02-17 20:27:57

0

更改为以下行:

document.getElementById("one").innerHTML = "<a href=\"logout.php\">Logout</a>"; 

“字符来转义 和环节都需要这样的

<a href="#" onClick="logedin()">link</a> 

值得使用ie 。像JS控制台那样的firefox插件,你可以看到发生什么行错误。

+0

这仍然不起作用 – 2015-02-17 20:30:11

+0

这只解决语法错误,而不是加载html的问题,以便php脚本可以将其定位。 – floor 2015-02-17 20:31:53

+0

链接不应该是另一个文件,而是JS内容,即onClick =“logedin()” – jmmk 2015-02-17 20:31:53

1

看,我会给你一些提示,你可以使用开发这样的:

  • 首先,你应该只使用PHP文件(index.php而不是index.html),所以它更容易管理POST数据和会话变量。

所以:

的index.php

<?php 
    // This is PHP code, executed BEFORE any output is sent. 

    // First, to save data that works across page loads, we should use sessions, so we start a session that has to be called in every PHP page that uses that information. 
    // Variables use the format $_SESSION['variable_name'] = value 
    session_name('MySession'); // Give it a unique name 
    session_start(); // Start a session 
?> 
<html> 
    <head> 
     <title>Some title for your page...</title> 
    </head> 
    <body> 
     <!-- Here you will manage your template. It's plain HTML but, as this is a PHP file, you can include PHP code as well inside the PHP tags --> 

     <?php 
      // This is a PHP tag, here we can manage some PHP and output different HTML 
      // We check if the user logged in or not 
      if (
       isset($_SESSION['logged_in']) // Always check if a variable exists before checking its value, or PHP will complain 
       && 
       $_SESSION['logged_in'] == true 
      ) 
      { 
       // The user logged in, show a LOGOUT link 
       echo '<a href=logout.php>Logout</a>'; 
      } 
      else 
      { 
       // Otherwise, the user did not log in. Show a link to log in. 
       echo '<a href=login.php>Login</a>'; 
      } 
     ?> 

     <!-- Any other HTML you want, template or whatever --> 
    </body> 
<html> 

现在,我们使用了两个文件:login.phplogout.php。第一个将显示一个表单,第二个将注销并重定向到索引页面。

的login.php

<html> 
    <head> 
     <title>Please log in</title> 
    </head> 
    <body> 
     <form action="do_login.php" method="post"><!-- Notice another file: do_login.php --> 
      <input type="text" name="username" placeholder="Your username" /> 
      <br /> 
      <input type="password" name="password" placeholder="Your password" /> 
      <br /> 
      <br /> 
      <input type="submit" name="submit" value="Log in" /> 
     </form> 
    <body> 
</html> 

现在我们需要的是处理登录(do_login文件。表格中的php)并存储会话数据。

do_login.php

<?php 
    // We use the same session as before 
    session_name('MySession'); // Same name as index.php and all other files 
    session_start(); 

    // This will be a pure PHP file that stores session data and returns to the index page. 
    // You want to check data against databases here, but we will use static information for easier reading. 
    // You also want to check data to be correct, but we won't do that here for simplicity. 

    $username = $_POST['username']; // This is the "username" from the form. 
    $password = $_POST['password']; // This is the "password" from the form. 

    if (
     $username == 'John' // Username is John 
     && 
     $password == 'MyPassword' // Password is MyPassword 
    ) 
    { 
     // Here the login data is correct, let's save some session variable that says the user correctly logged in. 
     // Note that this is potentially extremely INSECURE! You should save other data and check every request, but this is just for you to start learning. 
     $_SESSION['logged_in'] = true; 

     // Ok, user logged in. Redirect to the index. 
     header('Location: index.php'); // Send a redirect header (note that NOTHING has been echoed before in this page). 
     exit; 
    } 
    else 
    { 
     // Login data incorrect. Redirect to an error page, let's say login_error.php 
     header('Location: login_error.php'); 
     exit; 
    } 
?> 

现在文件注销:

logout.php

<?php 
    // First we recreate the session and destroy the variable(s) that say the user has logged in. 
    session_name('MySession'); // Same name as before 
    session_start(); // We start the session. At this point, all session variables have been recreated. 

    unset($_SESSION['logged_in']); // We destroy the variable 

    session_destroy(); // Now we drop the session 

    header('Location: index.php'); // Redirect to index.php 
    exit; 
?> 

现在,我们只需要登录失败页面:

login_error.php

<html> 
    <head> 
     <title>Login error!<title> 
    </head> 
    <body> 
     <h1>Login error!</h1> 
     <p>The login data was incorrect. Try again.</p> 
     <br /> 
     <p><a href="index.php">Go back to the index page</a></p> 
    </body> 
</html> 

我希望这可以帮助,但你真的需要阅读一些教程。 玩得开心!

+0

谢谢。我已经明白了。 :) – 2015-02-17 21:54:03

相关问题