英文:
Link submit button to another page
问题
我想将一个提交按钮链接到另一个PHP页面。但是,在表单标签中使用action不允许接下来的PHP代码被执行。我无法获取变量$_SESSION['logged']的值为1。是因为页面直接转到"messages.php"之前整个代码都被执行了吗?
英文:
I want to link a submit button to another PHP page. But using action in form tag doesn't allow the PHP code that comes next to be executed. I don't get the variable $_SESSION['logged']'s value to be 1. Is it because the page directly goes to the "messages.php" before the whole code is executed?
<?php
session_start();
?>
<!DOCTYPE html >
<html >
<body class="main">
<form action="messages.php" method="post">
<p><center>Username : <input type="text" name="user" class="form-control"/></center></p>
<p><center>Password : <input type="password" name="pswd" class="form-control"/></center></p>
<p><center><input type="submit" value="Log in" class="btn btn-primary"/></center></p>
</div>
</form>
<?php
$_SESSION['logged']=0;
$_SESSION['user'] = '';
$_SESSION['pswd'] = '';
if (isset($_POST['user']) AND isset($_POST['pswd']) )
{
$user=$_POST['user'];
$pswd=$_POST['pswd'];
$account = new Account();
try {
$logged = $account->LogIn($user, $pswd);
} catch (Exception $e) {
/* Something went wrong: echo the exception message and die */
echo $e->getMessage();
die();
}
$_SESSION['logged']=1; #1 means logged in
}
?>
</body>
</html>
答案1
得分: 2
以下是您要翻译的内容:
<form action="?" method="post">
<p><center>用户名:<input type="text" name="user" class="form-control"/></center></p>
<p><center>密码:<input type="password" name="pswd" class="form-control"/></center></p>
<p><center><input type="submit" value="登录" class="btn btn-primary"/></center></p>
</form>
然后,在应用业务逻辑后重定向到 messages.php:
<?php
$_SESSION['logged'] = 0;
$_SESSION['user'] = '';
$_SESSION['pswd'] = '';
if (isset($_POST['user']) AND isset($_POST['pswd']))
{
$user = $_POST['user'];
$pswd = $_POST['pswd'];
$account = new Account();
try
{
$logged = $account->LogIn($user, $pswd);
$_SESSION['logged'] = 1; #1 表示已登录
$script = "<script>
window.location = 'messages.php';</script>";
echo $script;
}
catch (Exception $e)
{
/* 出现问题:输出异常消息并终止 */
echo $e->getMessage();
die();
}
}
?>
英文:
The form should be with the action on the page you are making the logic, which means the present one:
<form action="?" method="post">
<p><center>Username : <input type="text" name="user" class="form-control"/></center></p>
<p><center>Password : <input type="password" name="pswd" class="form-control"/></center></p>
<p><center><input type="submit" value="Log in" class="btn btn-primary"/></center></p>
</div>
</form>
And then, after applying busines logic redirect to messages.php:
<?php
$_SESSION['logged']=0;
$_SESSION['user'] = '';
$_SESSION['pswd'] = '';
if (isset($_POST['user']) AND isset($_POST['pswd']) )
{
$user=$_POST['user'];
$pswd=$_POST['pswd'];
$account = new Account();
try
{
$logged = $account->LogIn($user, $pswd);
$_SESSION['logged']=1; #1 means logged in
$script = "<script>
window.location = 'messages.php';</script>";
echo $script;
}
catch (Exception $e)
{
/* Something went wrong: echo the exception message and die */
echo $e->getMessage();
die();
}
}
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论