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

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

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?

  1. <?php
  2. session_start();
  3. ?>
  4. <!DOCTYPE html >
  5. <html >
  6. <body class="main">
  7. <form action="messages.php" method="post">
  8. <p><center>Username : <input type="text" name="user" class="form-control"/></center></p>
  9. <p><center>Password : <input type="password" name="pswd" class="form-control"/></center></p>
  10. <p><center><input type="submit" value="Log in" class="btn btn-primary"/></center></p>
  11. </div>
  12. </form>
  13. <?php
  14. $_SESSION['logged']=0;
  15. $_SESSION['user'] = '';
  16. $_SESSION['pswd'] = '';
  17. if (isset($_POST['user']) AND isset($_POST['pswd']) )
  18. {
  19. $user=$_POST['user'];
  20. $pswd=$_POST['pswd'];
  21. $account = new Account();
  22. try {
  23. $logged = $account->LogIn($user, $pswd);
  24. } catch (Exception $e) {
  25. /* Something went wrong: echo the exception message and die */
  26. echo $e->getMessage();
  27. die();
  28. }
  29. $_SESSION['logged']=1; #1 means logged in
  30. }
  31. ?>
  32. </body>
  33. </html>

答案1

得分: 2

以下是您要翻译的内容:

  1. <form action="?" method="post">
  2. <p><center>用户名:<input type="text" name="user" class="form-control"/></center></p>
  3. <p><center>密码:<input type="password" name="pswd" class="form-control"/></center></p>
  4. <p><center><input type="submit" value="登录" class="btn btn-primary"/></center></p>
  5. </form>
  6. 然后,在应用业务逻辑后重定向到 messages.php:
  7. <?php
  8. $_SESSION['logged'] = 0;
  9. $_SESSION['user'] = '';
  10. $_SESSION['pswd'] = '';
  11. if (isset($_POST['user']) AND isset($_POST['pswd']))
  12. {
  13. $user = $_POST['user'];
  14. $pswd = $_POST['pswd'];
  15. $account = new Account();
  16. try
  17. {
  18. $logged = $account->LogIn($user, $pswd);
  19. $_SESSION['logged'] = 1; #1 表示已登录
  20. $script = "<script>
  21. window.location = 'messages.php';</script>";
  22. echo $script;
  23. }
  24. catch (Exception $e)
  25. {
  26. /* 出现问题:输出异常消息并终止 */
  27. echo $e->getMessage();
  28. die();
  29. }
  30. }
  31. ?>
英文:

The form should be with the action on the page you are making the logic, which means the present one:

  1. &lt;form action=&quot;?&quot; method=&quot;post&quot;&gt;
  2. &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;
  3. &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;
  4. &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;
  5. &lt;/div&gt;
  6. &lt;/form&gt;

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

  1. &lt;?php
  2. $_SESSION[&#39;logged&#39;]=0;
  3. $_SESSION[&#39;user&#39;] = &#39;&#39;;
  4. $_SESSION[&#39;pswd&#39;] = &#39;&#39;;
  5. if (isset($_POST[&#39;user&#39;]) AND isset($_POST[&#39;pswd&#39;]) )
  6. {
  7. $user=$_POST[&#39;user&#39;];
  8. $pswd=$_POST[&#39;pswd&#39;];
  9. $account = new Account();
  10. try
  11. {
  12. $logged = $account-&gt;LogIn($user, $pswd);
  13. $_SESSION[&#39;logged&#39;]=1; #1 means logged in
  14. $script = &quot;&lt;script&gt;
  15. window.location = &#39;messages.php&#39;;&lt;/script&gt;&quot;;
  16. echo $script;
  17. }
  18. catch (Exception $e)
  19. {
  20. /* Something went wrong: echo the exception message and die */
  21. echo $e-&gt;getMessage();
  22. die();
  23. }
  24. }
  25. ?&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:

确定