如何从不同的html / php文件中记录document.getElementById()。innerHTML

时间:2015-02-17 20:20:09

标签: javascript php html

你好我对编码很新,但我得到了我需要的基础知识。

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

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

4 个答案:

答案 0 :(得分: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>

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

答案 1 :(得分:0)

使用已使用的"代替您应该使用'

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

答案 2 :(得分:0)

更改为以下行:

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

“必须转义字符。 链接必须像那样

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

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

答案 3 :(得分: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文件。