gameOver){ doSomething(); $this->playerChoice=$this->getPlayerInput(); doSomethingElse(); } } 公共函数getPlayerInput(){ echo',php,methods,form-submit,Php,Methods,Form Submit" /> gameOver){ doSomething(); $this->playerChoice=$this->getPlayerInput(); doSomethingElse(); } } 公共函数getPlayerInput(){ echo',php,methods,form-submit,Php,Methods,Form Submit" />

如何在PHP方法的中间获得用户输入? 我用类写了一个PHP游戏,我想知道如何在主游戏方法的中间获得用户输入。我理解从HTML表单提交数据的概念,PHP脚本使用 $yGET或 $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; } } 类游戏 { $playerChoice=“”; $gameOver=“false”; 公共职能扮演{ 同时!($this->gameOver){ doSomething(); $this->playerChoice=$this->getPlayerInput(); doSomethingElse(); } } 公共函数getPlayerInput(){ echo'

如何在PHP方法的中间获得用户输入? 我用类写了一个PHP游戏,我想知道如何在主游戏方法的中间获得用户输入。我理解从HTML表单提交数据的概念,PHP脚本使用 $yGET或 $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; } } 类游戏 { $playerChoice=“”; $gameOver=“false”; 公共职能扮演{ 同时!($this->gameOver){ doSomething(); $this->playerChoice=$this->getPlayerInput(); doSomethingElse(); } } 公共函数getPlayerInput(){ echo',php,methods,form-submit,Php,Methods,Form Submit,您对HTTP请求如何工作的基本原理存在误解 您的脚本应该独立地处理表单的每次提交。我的意思是,脚本应该处理请求、显示表单并终止执行。然后,当用户点击提交时,您的脚本将再次执行。您需要使用类似或的方式在用户请求之间保留状态。您有一个错误有关HTTP请求如何工作的基本信息 您的脚本应该独立处理表单的每次提交。我的意思是,脚本应该处理请求、显示表单并终止执行。然后,当用户点击提交时,您的脚本将再次执行。您需要使用类似或的方式在用户请求之间保留状态。通常是输入取自向服务器发送请求的用户或ajax(链接或

您对HTTP请求如何工作的基本原理存在误解


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

您有一个错误有关HTTP请求如何工作的基本信息


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

通常是输入取自向服务器发送请求的用户或ajax(链接或ajax对象)。您应该在一个页面上完成函数的前半部分,在第二个页面上完成(通过链接或其他方式完成)

公共功能播放(){
同时!($this->gameOver){
doSomething();
}
}
公共函数getPlayerInput(){

echo'通常输入来自向服务器发送请求的用户或ajax(链接或ajax对象)。您应该在一个页面上完成函数的前半部分,在第二个页面上完成(通过链接或其他方式完成)

公共功能播放(){
同时!($this->gameOver){
doSomething();
}
}
公共函数getPlayerInput(){

echo'您必须更好地掌握输入在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...';
  }

}
类游戏
{
$playerChoice=“”;
$gameOver=“false”;
公共函数pendingInput(){
//检查是否输入到$\u GET或$\u POST并返回数据或false
//类似于(此函数可能更复杂(或拆分)
//分为更小的部分):
如果(isset($_POST['submit'])){
//始终检查用户输入是否存在伪造
//显然,您必须编写sanitize函数
$input=消毒($_POST['submit']);
返回$input;
}否则{
返回false;
}
}
公共职能扮演{
如果(!$this->gameOver){
$input=$this->pendingInput();
如果($输入){
$this->applyUserMove($input);
$this->doComputerMove();
}
$this->getPlayerInput();
}否则{
$this->gameEnds();
}
}
公共函数getPlayerInput(){

echo'您必须更好地掌握输入在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...';
  }

}
类游戏
{
$playerChoice=“”;
$gameOver=“false”;
公共函数pendingInput(){
//检查是否输入到$\u GET或$\u POST并返回数据或false
//类似于(此函数可能更复杂(或拆分)
//分为更小的部分):
如果(isset($_POST['submit'])){
//始终检查用户输入是否存在伪造
//显然,您必须编写sanitize函数
$input=消毒($_POST['submit']);
返回$input;
}否则{
返回false;
}
}
公共职能扮演{
如果(!$this->gameOver){
$input=$this->pendingInput();
如果($输入){
$this->applyUserMove($input);
$this->doComputerMove();
}
$this->getPlayerInput();
}否则{
$this->gameEnds();
}
}
公共函数getPlayerInput(){

echo“我理解从HTML表单提交数据的概念”-不,你不理解。否则你知道你可以在脚本的任何位置从$\u GET或$\u POST检索数据。用户没有提交数据,因为你的脚本假设用户在获得表单提交之前已经发送了数据。“我理解从HTML表单提交数据的概念"---不,您不需要。否则您知道您可以在脚本的任何位置从$\u GET或$\u POST检索数据。用户没有提交数据,因为您的脚本假设用户在获得表单提交之前已经发送了数据。好的,但是第二个页面是否可以访问运行getPlayerIn的游戏对象的属性put(),或者我需要创建一个会话来提供这些信息吗?@sigil您必须使用会话,因为http是一个无状态协议。即使是第二次访问原始页面,如果没有sessionsok,也无法访问属性,但是第二次访问页面是否可以访问运行getPlayerInput()的游戏对象的属性,或者我需要创建一个会话来提供这些信息吗?@sigil您必须使用会话,因为http是一个无状态协议