单击提交按钮时,使用$ _POST从<select>字段获取输出

问题描述:

I'm making a rock paper scissors game and players need to choose if they want to play best out of 1, 3, 5 or 7 before they start the game and i need it to work with a Select field and a submit button.

I am very new to the select tag but it would suit me best if the selected number could be exctracted with $_POST or $_GET

This is the form:

<form method="post">
<h1>Best out of </h1>
<select>
    <option value="one">1</option>
    <option value="three">3</option>
    <option value="five">5</option>
    <option value="seven">7</option>
</select>
<input type="submit" name="start" value="START" />
</form>

This is the PHP:

<?php
    if(isset($_POST['start']) && isset($_POST['one']))
    {
        echo "do something 1";
    };
    if(isset($_POST['start']) && isset($_POST['three']))
    {
        echo "do something 2";
    };
    if(isset($_POST['start']) && isset($_POST['five']))
    {
        echo "do something 3";
    };
    if(isset($_POST['start']) && isset($_POST['seven']))
    {
        echo "do something 4";
    };
?>

我正在制作一个石头剪刀游戏,玩家需要选择是否想要在1中发挥最佳效果, 在他们开始游戏之前我需要使用3,5或7,我需要它来使用选择字段和提交按钮。 p>

我对选择标签很新,但它最适合我 如果所选的号码可以用$ _POST或$ _GET p>提取

这是表格: strong> p>

   &lt; form method =“post”&gt; 
&lt; h1&gt; Best of&lt; / h1&gt; 
&lt; select&gt; 
&lt; option value =“one”&gt; 1&lt; / option&gt; 
&lt; option  value =“three”&gt; 3&lt; / option&gt; 
&lt; option value =“five”&gt; 5&lt; / option&gt; 
&lt; option value =“seven”&gt; 7&lt; / option&gt; 
&lt; / 选择&gt; 
&lt; input type =“submit”name =“start”value =“START”/&gt; 
&lt; / form&gt; 
  code>  pre> 
 
 

这是PHP: strong> p>

 &lt;?php 
 if(isset($ _ POST ['start'])&amp;&amp; isset($ _ POST [  'one']))
 {
 echo“做一些事情1”; 
  }; 
 if(isset($ _ POST ['start'])&amp;&amp;  isset($ _ POST ['three']))
 {
 echo“do something 2”; 
}; 
 if(isset($ _ POST ['start'])&amp;&amp; isset($ _ POST [  'five']))
 {
 echo“做一些事情3”; 
}; 
 if(isset($ _ POST ['start'])&amp;&amp; isset($ _ POST ['seven'])  )
 {
 echo“做点什么4”; 
}; 
?&gt; 
  code>  pre> 
  div>

Give your select a name attribute

<form method="post">
<h1>Best out of </h1>
<select name="my_select">
    <option value="one">1</option>
    <option value="three">3</option>
    <option value="five">5</option>
    <option value="seven">7</option>
</select>
<input type="submit" name="start" value="START" />
</form>

After submitting your form in the PHP script you can get select value using $_POST array with index my_select ( name attribute value of the select element):

<?php
if(isset($_POST['start']) && isset($_POST['my_select']))
{
    if($_POST['my_select'] === 'one'){}
    ....
}
?>