html form wont submit POST

I’m workin on a Rock Paper Scissors game to practice my html and php, everythig was fine until I tried to submit a user-selected answer in a POST. I´ve been checking and nothig is submitted when the button Play is pressed.

This is my html code:

<html>     <head>         <title>Oscar Felipe Ramírez Pardo</title>         <meta charset="UTF-8">     </head>     <body>         <h1>Rock Paper Scissors</h1>         <p>Welcome: <?=htmlentities($_GET['name'])?></p>         <forms method="POST">             <p>                 <select name="option">                     <option value="Select" checked>Select</option>                     <option value="Rock">Rock</option>                     <option value="Paper">Paper</option>                     <option value="Scissors">Scissors</option>                     <option value="Check">Check</option>                 </select>                 <input type="submit" value="Play" name="dopost">                 <input type="button" value="Logout" name="logout"></p>         </forms>         <pre> <?php foreach($rslt as $k => $v){     echo $v."\n"; } ?>         </pre>      </body> </html> 

And this is the model control before it:

<?php     $rslt=array("Please select a strategy and press Play.");     $names=array("Rock","Paper","Scissors");      if(!isset($_GET['name'])) die("Name parameter missing");     if(isset($_POST['logout'])){         header("Location: login.php");         return;     }      function play($h){         $c=$names[rand(0,2)];         if($h===$c) $r="Tie";         else if($h==="Rock" && $c==="Scissors") $r="You win.";         else if($h==="Scissors" && $c==="Paper") $r="You win.";         else if($h==="Paper" && $c==="Rock") $r="You win.";         else $r="You lose.";         return "Your Play=$h Computer Play=$c Result=$r";     }                if(isset($_POST['option'])){         if($_POST['option']==="Select") $rslt=array("You must select a strategy in order to play.");         else if($_POST['option']==="Check"){          }         else{             $rslt=array(play($_POST['option']));         }     } ?> 

I don´t get anything when pressing either button as it shows here., but the source code shows the first php variables are being recognized.

Add Comment
3 Answer(s)

I think that you have misspelled the tag <form> with <forms> amd you have to add an actions to specify the php page you want to call.

Like this:

<form method="POST" action="test.php">             <p>                 <select name="option">                     <option value="Select" checked>Select</option>                     <option value="Rock">Rock</option>                     <option value="Paper">Paper</option>                     <option value="Scissors">Scissors</option>                     <option value="Check">Check</option>                 </select>                 <input type="submit" value="Play" name="dopost">                 <input type="button" value="Logout" name="logout"></p>         </form>

Answered on July 16, 2020.
Add Comment

It’s not only forms thats wrong, you shouls also have an undefined error when you play. Add global $names in your play function. and add the result in a p or other tag instead of textarea is better to read

$rs = 1; $rslt = "Please select a strategy and press Play."; $names = array("Rock", "Paper", "Scissors");  if (!isset($_GET['name'])) die("Name parameter missing"); if (isset($_POST['logout'])) {   header("Location: login.php");   return; }  function play($h) {   global $names;   $c = $names[rand(0, 2)];   if ($h === $c) $r = "Tie";   else if ($h === "Rock" && $c === "Scissors") $r = "You win.";   else if ($h === "Scissors" && $c === "Paper") $r = "You win.";   else if ($h === "Paper" && $c === "Rock") $r = "You win.";   else $r = "You lose.";   return "Your Play=$h Computer Play=$c Result=$r"; }   if (isset($_POST['option'])) {   if ($_POST['option'] === "Select") $rslt = "You must select a strategy in order to play.";   else if ($_POST['option'] === "Check") {    } else {     $rslt = play($_POST['option']);   } }  ?>  <html> <head>   <title>Oscar Felipe Ramírez Pardo</title>   <meta charset="UTF-8"> </head> <body> <h1>Rock Paper Scissors</h1> <p>Welcome: <?= htmlentities($_GET['name']) ?></p> <form method="POST">      <select name="option">       <option value="Select" >Select</option>       <option value="Rock">Rock</option>       <option value="Paper">Paper</option>       <option value="Scissors">Scissors</option>       <option value="Check">Check</option>     </select>     <input type="submit" value="Play" name="dopost">     <input type="button" value="Logout" name="logout">   <p style="font-family:monospace">                 <p> <?= $rslt ?>                 </p>   </p> </form> </body> </html> 
Add Comment

I actually found too that the Logout button won’t work since it’s type is button and not submit therfore the post wasn’t submitted and the isset($_POST['logout']) never worked.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.