gpt4 book ai didi

javascript - 如何从不同的 html/php 文件记录.getElementById().innerHTML

转载 作者:行者123 更新时间:2023-12-03 10:45:59 26 4
gpt4 key购买 nike

您好,我对一般编码非常陌生,但我已经掌握了我需要的基础知识。

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

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

最佳答案

看,我会给你一些可以用来开发这个的技巧:

  • 首先,您应该仅使用 PHP 文件( index.php 而不是 index.html ),这样可以更轻松地管理 POST 数据和 session 变量。

所以:

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)并存储 session 数据。

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>

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

关于javascript - 如何从不同的 html/php 文件记录.getElementById().innerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com