如何从不同的html.php文件中记录.getElementById().inerHTML


how to document.getElementById().innerHTML from different html/php file

大家好,我对编码很陌生,但我已经掌握了所需的基本知识。

我有一个包含以下内容的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>

请记住,我是从我正在编辑的网站模板中得到的,所以我没有想出这个布局

我有一个php文件,里面有一些html,当运行这部分代码时,它可以尝试替换列表的内容:

<?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
                    }
                ?> 

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

听着,我会给你一些开发的技巧

  • 首先,您应该只使用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>

我希望这能有所帮助,但你确实需要阅读一些教程。玩得高兴

使用已使用的",而应该使用'

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

更改为以下行:

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

"必须转义字符。链接必须像一样

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

值得使用的是,比如像JS控制台这样的firefox插件,您可以在其中看到发生的行错误。

我已经弄明白了。在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文件。