链接提交按钮到另一个页面。

huangapple go评论61阅读模式
英文:

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:

&lt;form action=&quot;?&quot; method=&quot;post&quot;&gt;
            &lt;p&gt;&lt;center&gt;Username : &lt;input type=&quot;text&quot; name=&quot;user&quot; class=&quot;form-control&quot;/&gt;&lt;/center&gt;&lt;/p&gt;
            &lt;p&gt;&lt;center&gt;Password : &lt;input type=&quot;password&quot; name=&quot;pswd&quot; class=&quot;form-control&quot;/&gt;&lt;/center&gt;&lt;/p&gt;
            &lt;p&gt;&lt;center&gt;&lt;input type=&quot;submit&quot;  value=&quot;Log in&quot; class=&quot;btn btn-primary&quot;/&gt;&lt;/center&gt;&lt;/p&gt;
        &lt;/div&gt;
        
        &lt;/form&gt;

And then, after applying busines logic redirect to messages.php:

    &lt;?php

$_SESSION[&#39;logged&#39;]=0;

$_SESSION[&#39;user&#39;] = &#39;&#39;;
$_SESSION[&#39;pswd&#39;] = &#39;&#39;;

if (isset($_POST[&#39;user&#39;]) AND isset($_POST[&#39;pswd&#39;]) )
{
    $user=$_POST[&#39;user&#39;];
    $pswd=$_POST[&#39;pswd&#39;];


$account = new Account();
try
{
    $logged = $account-&gt;LogIn($user, $pswd);
    $_SESSION[&#39;logged&#39;]=1; #1 means logged in

$script = &quot;&lt;script&gt;
window.location = &#39;messages.php&#39;;&lt;/script&gt;&quot;;
echo $script;


}
catch (Exception $e)
{
    /* Something went wrong: echo the exception message and die */
    echo $e-&gt;getMessage();
    die();
}


}

?&gt;

huangapple
  • 本文由 发表于 2023年6月1日 03:31:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定