简体   繁体   English

PHP不输出数组

[英]PHP not outputting Array

so I wanted to try some backend style code and ive done some messing around with PHP.所以我想尝试一些后端风格的代码,我对 PHP 做了一些处理。 I've made a simple program (using html and php) that asks the user to create an account, then these usernames and passwords will be stored.我制作了一个简单的程序(使用 html 和 php),要求用户创建一个帐户,然后将存储这些用户名和密码。

Im adding a feature (at the bottom) that asks if the user wants to display a certain username (un) and password (pw).我添加了一个功能(在底部),询问用户是否想要显示某个用户名(un)和密码(pw)。 However, when i enter some random un's and pw's, and type 1 (so program should output un 1 and pw 1) it does not work...但是,当我输入一些随机的 un 和 pw 并键入 1(因此程序应输出 un 1 和 pw 1)时,它不起作用...

Any help would be grately appreciated!任何帮助将不胜感激!

<html>
  <body>
    <style>
      input {
        font-family: 'Trebuchet MS'
      }
      form {
        font-family: 'Trebuchet MS'
      }
      body {
        background-color: lightblue

      }
      h1 {
        font-family: 'Trebuchet MS'

      }
    </style>
    <h1 id = "signup_here">Signup here!</h1>
    <form action = "[the website uses my real name, so im not including it]" method = "get">
      Your Username: <input type = "text" name = "username">
      <br/>
      Your Password: <input type = "text" name = "password">
      <input type = "submit">
    </form>
    <br/>
    <?PHP
      $username = $_GET["username"];
      $password = $_GET["password"];
      if(!is_null($username) and !is_null($password)){
        echo "<h3>Your username is [$username] and your password is [$password]";
      }
    ?>
    <form method = "post">
      Please confirm: <input type = "checkbox" name = "up_conf" value = "val1">
      <input type = "submit">
    </form>
    <?PHP
      if(isset($_POST["up_conf"])) {
        echo "<h3>Thanks for signing up!</h3>";
      }
    ?>
    <?PHP
      if(isset($_POST["up_conf"])) {
        $allusernames = array($_GET["username"]);
        $allpasswords = array($_GET["password"]);
      }
    ?>
    <form action = "https://PhP-Playground.---.repl.co" method = "get">
      Get usernames and passwords? 1/2/3/etc.: <input type = "text" name = "gaup">
      <?PHP
        if(!is_null($_GET["gaup"])) {
          echo "Username:" . $allusernames[$_GET["gaup"]];
          echo "Password:" . $allpassword[$_GET["gaup"]];
        }
      ?>
    </form>
  </body>
</html>

I think you have a handful of things going on here and you may need to simplify them to get to the root of the particular problem.我认为这里有一些事情要做,您可能需要简化它们才能找到特定问题的根源。

A couple basic things:一些基本的事情:

  1. You don't need a full URL as the action parameter of your <form> -- you can (and often should) use relative URLs.您不需要完整的 URL 作为<form>的操作参数——您可以(并且通常应该)使用相对 URL。 Eg <form action="/some-page" method="post"> -- that way you wouldn't need to censor the URL.例如<form action="/some-page" method="post"> -- 这样你就不需要审查 URL。

  2. When debugging forms, stick to one form to page -- that will help avoid confusion between $_GET and $_POST调试表单时,坚持一个表单到页面——这将有助于避免$_GET$_POST之间的混淆

  3. A helpful debugging tactic is to use print_r() to show the entire contents of a variable.一个有用的调试策略是使用print_r()来显示变量的全部内容。 This can be combined with HTML's <pre> tag for easier readability, eg这可以与 HTML 的<pre>标签结合使用以提高可读性,例如

<pre>
<?php
   print_r($_POST);
?>
</pre>
  1. Don't use <?PHP -- instead stick to the lower-case <?php -- I know PHP permits some really sloppy carelessness when it comes to capitalization, but I know I've been on servers that were configured in a way that things like short codes would not run.不要使用<?PHP -- 而是坚持使用小写的<?php -- 我知道 PHP 在大小写方面允许一些非常草率的粗心大意,但我知道我一直在以某种方式配置的服务器上诸如短代码之类的东西将无法运行。

  2. Don't submit login forms using get -- use post .不要使用get提交登录表单——使用post

Hopefully that helps you see where your data is going.希望这可以帮助您了解数据的去向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM