如何在 PHP 方法中获取用户输入?

发布于 2024-10-18 03:48:45 字数 909 浏览 1 评论 0原文

我正在使用类编写一个 PHP 游戏,并且我正在尝试弄清楚如何在主要游戏方法中获取用户输入。我了解从 HTML 表单提交数据并让 PHP 脚本使用 $_GET$_POST 检索该数据的概念,但我不知道如何做到这一点发生在方法的中间(如果这是处理这个问题的最佳方法)。这是游戏玩法类:

class GamePlay
{
    $playerChoice="";
    $gameOver="false";

    public function play() {
        while !($this->gameOver) {
            doSomething();
            $this->playerChoice=$this->getPlayerInput();
            doSomethingElse();
        }
    }

    public function getPlayerInput() {
        echo '<form name="playerForm" "method="post">';
            echo '<input type="submit" name="submit" value="do this" />';
            echo '<input type="submit" name="submit" value="do that" />';
        echo '</form>';

        $input=$_POST['submit'];
        return $input;
    }
}

但是脚本最终执行 doSomethingElse() 而不等待用户提交数据,并且我希望用户在脚本继续之前提交。我该怎么办?

I'm writing a PHP game using classes, and I'm trying to figure out how to get user input in the middle of the main gameplay method. I understand the concept of submitting data from an HTML form and having the PHP script retrieve that data using $_GET or $_POST, but I don't know how to have this happen in the middle of the method (if that is the best way to handle this). Here's the gameplay class:

class GamePlay
{
    $playerChoice="";
    $gameOver="false";

    public function play() {
        while !($this->gameOver) {
            doSomething();
            $this->playerChoice=$this->getPlayerInput();
            doSomethingElse();
        }
    }

    public function getPlayerInput() {
        echo '<form name="playerForm" "method="post">';
            echo '<input type="submit" name="submit" value="do this" />';
            echo '<input type="submit" name="submit" value="do that" />';
        echo '</form>';

        $input=$_POST['submit'];
        return $input;
    }
}

But the script ends up executing doSomethingElse() without waiting for the user to submit data, and I want the user to submit before the script moves on. What should I do instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫卆 2024-10-25 03:48:45

您对 HTTP 请求工作原理的基础知识存在误解。

您的脚本应该独立处理表单的每次提交。我的意思是脚本应该处理请求、显示表单并终止执行。然后,当用户点击“提交”时,您的脚本将再次执行。您将需要使用类似 sessionscookie 用于保留用户请求之间的状态。

You have a misconception about the basics of how HTTP requests work.

Your script should deal with each submission of the form independently. In that I mean the script should process the request, display the form, and terminate execution. Then when the user hits submit your script will be executed again. You will need to use something like sessions or cookies to preserve state between user requests.

沙与沫 2024-10-25 03:48:45

通常输入来自用户或向服务器发送请求的 ajax(链接或 ajax 对象)。您应该将函数的前半部分放在一页上,并将完成部分放在第二页上(通过链接或其他东西到达)。

public function play(){
  while !($this->gameOver){
   doSomething();
  }
}

public function getPlayerInput(){
  echo '<form name="playerForm" "method="post" action="action.php">';
  echo '<input type="submit" name="submit" value="do this" />';
  echo '<input type="submit" name="submit" value="do that" />';
  echo '</form>';
  $input=$_POST['submit'];
  return $input;
}

然后第二个页面action.php可以通过读取$_POST变量并用它做一些事情来处理。

Usually input is taken from the user or ajax that sends a request to the server (either a link or ajax object). You should have the first half of your function on one page and a completion on the second page (reached by a link or something).

public function play(){
  while !($this->gameOver){
   doSomething();
  }
}

public function getPlayerInput(){
  echo '<form name="playerForm" "method="post" action="action.php">';
  echo '<input type="submit" name="submit" value="do this" />';
  echo '<input type="submit" name="submit" value="do that" />';
  echo '</form>';
  $input=$_POST['submit'];
  return $input;
}

Then a second page action.php can process by reading $_POST variable and doSomethingElse with it.

淡笑忘祈一世凡恋 2024-10-25 03:48:45

你必须更好地掌握 php 中的输入是如何工作的。

您不能让用户将数据输入到脚本中间。当 php 脚本开始执行时,所有用户输入都已完成,您只需处理它即可。

尝试将您的方法分成两个来模拟预期的行为

class GamePlay
{
  $playerChoice="";
  $gameOver="false";

  public function pendingInput() {
    // check for input into $_GET or $_POST and return data or false
    // something like (this function maybe a lot more complicate (or splitted
    // into smaller ones): 
      if (isset($_POST['submit']) {
         // always check user input for forgeries
         // you have to write the sanitize function obviously
         $input=sanitize($_POST['submit']); 
        return $input;
      } else {
        return false;
      }
  }

  public function play(){
      if (!$this->gameOver) {
         $input =  $this->pendingInput();
         if($input) {
            $this->applyUserMove($input);
            $this->doComputerMove();
         }
         $this->getPlayerInput();
      } else {
         $this->gameEnds();
      }
  }

  public function getPlayerInput(){
      echo '<form name="playerForm" "method="post">';
      echo '<input type="submit" name="submit" value="do this" />';
      echo '<input type="submit" name="submit" value="do that" />';
      echo '</form>';
  }

  public function gameEnds() {
      echo 'The game is over. The winner is blah blah ... ';
      echo 'play another game?';
      echo 'link to start game...';
  }

}

您必须考虑您在问题中设计的 while 循环的单次运行。使用会话(因为 http 是无状态的)并让服务器/客户端处理循环。

注意:这只是解决问题的一种方法,还有其他方法,但这与您想要的解决方案类似。

一些额外的提示:您可以将 php 游戏视为 有限状态自动机

  • 您从外部获取输入
  • ,更改内部状态
  • 输出,以通知用户您的新状态,
  • 最终等待输入

您可以使用调度程序来选择status 并为每个状态编写一个方法,或者只执行像 applyUserMove 这样的厨房水槽方法并在内部操作开关。无论哪种方式,您必须做的工作(作为程序)都是相同的。

You have to get a better grasp on how the input works in php.

You can't have the user to input data into the middle of a script. When the php script start to execute all the user input is already done and you have just to process it.

Try to split your method into two to simulate the intended behaviour

class GamePlay
{
  $playerChoice="";
  $gameOver="false";

  public function pendingInput() {
    // check for input into $_GET or $_POST and return data or false
    // something like (this function maybe a lot more complicate (or splitted
    // into smaller ones): 
      if (isset($_POST['submit']) {
         // always check user input for forgeries
         // you have to write the sanitize function obviously
         $input=sanitize($_POST['submit']); 
        return $input;
      } else {
        return false;
      }
  }

  public function play(){
      if (!$this->gameOver) {
         $input =  $this->pendingInput();
         if($input) {
            $this->applyUserMove($input);
            $this->doComputerMove();
         }
         $this->getPlayerInput();
      } else {
         $this->gameEnds();
      }
  }

  public function getPlayerInput(){
      echo '<form name="playerForm" "method="post">';
      echo '<input type="submit" name="submit" value="do this" />';
      echo '<input type="submit" name="submit" value="do that" />';
      echo '</form>';
  }

  public function gameEnds() {
      echo 'The game is over. The winner is blah blah ... ';
      echo 'play another game?';
      echo 'link to start game...';
  }

}

You have to think on a single run of the while loop you designed in your question. Use sessions (as http is stateless) and let the server/client take care of the loop.

Notice: This is just an approach to the problem, there are others but this is similar to your intended solutions.

Some additional hints: You can think of a php game as a Finite State Automata:

  • you get input from outside
  • change your internal status
  • output for inform user of your new status
  • eventually wait for input

You can use a dispatcher to select the status and write a method for every state or just do a kitchen sink method like applyUserMove and operate the switch internally. Either way the work (as program) you have to do is the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文