POST随机变量

时间:2017-10-10 20:03:10

标签: php checkbox random

我有一个带有复选框的表单..我需要随机发送一个作为变量。

选中更多复选框并随机写一个:

<form action="index.php" method="POST">
 <input type="text" name="name" placeholder="Jméno vyvolávače" maxlength="32"><p>
 <table>
  <tr>
   <input type="checkbox" onClick="toggle(this)" /> Vyber vše<br/>
   <td>Ahri<input type="checkbox" name="champ" value="Ahri"></td>
   <td>Aatrox<input type="checkbox" name="champ" value="Aatrox"></td>
   <td>xx<input type="checkbox" name="champ" value="xx"></td>
   <td>xx<input type="checkbox" name="champ" value="xx"></td>
  </tr>
 </table>
 <input type="submit">
</form>
<?php
 echo "Vyvolávači ".$name," pickni si ".$champ," na ".$line;
?>

所以现在我检查Ahri和Aatrox ..并且页面随机提交一个。 我怎么能这样做?

1 个答案:

答案 0 :(得分:4)

您当前的代码无法使用,因为所有复选框都具有相同的名称。这意味着页面上的最后一个复选框将始终是表单发送的唯一值,因为它将被覆盖。

有一个解决方案!通过在变量名称中添加方括号([]),您告诉表单将​​值作为数组发送,以便所有选中的选项都是与表格一起发送。

如果查看下面的代码,我将方括号添加到“name”属性中。您可以找到有关表单元素标记的更多信息,以便成为数组on this StackOverflow post

<td>
    Ahri
    <input type="checkbox" name="champ[]" value="Ahri">
</td>
<td>
    Aatrox
    <input type="checkbox" name="champ[]" value="Aatrox">
</td>

现在,你可以使用shuffle()随机化数组,这样你就可以使用array_pop()从数组中取出第一个元素,此时它将是一个基于已选中的复选框。

如果未选中复选框,则不会将其值传递给表单,因此您可以在所选复选框之间随机选择。

if(isset($_POST['champ'])) {

    /*I assign the array to a regular PHP variable as it's considered 
    bad practice to alter GLOBAL variables. If no choices are selected,
    you will always get the value of "No Options Selected" because of the ternary.*/
    $champ = !empty($_POST['champ'])? $_POST['champ'] : ["No Options Selected"];

    /*shuffle the array, randomizing the location of each value*/
    shuffle($champ);

    /*array pop takes the first element off the array, and returns the value.
    So we set $randomValue to the first element in the array basically.*/
    $randomValue = array_pop($champ);

    /*You could also overwrite the array if you want to use 
    the $champ variable.*/
    //$champ = array_pop($champ); 

    /*alternatively, you could do `$randomValue = $champ[0]` for the same result, 
    except the array will not lose the first element.*/
    //$randomValue = $champ[0];

    /*You could also overwrite the array if you want to use 
    the $champ variable.*/
    //$champ = $champ[0];

}

$randomValue现在将包含基于所选复选框的随机值。

echo "Vyvolávači ".$name," pickni si ".$randomValue," na ".$line;

- 更新到此部分: 我更改了$champ的变量分配,以使用empty()检查$_POST['champ']复选框是ternary。这使得如果在表单页面中没有选中复选框,它将替换另一个只包含单个元素的数组,该元素显示“No Options Selected”,如果没有选择选项,这将始终是返回。

您正在使用PHP,为什么不使用它?

作为旁注,您可以通过使用具有可能值的循环来删除大量HTML。这可以防止大量拼写错误和语法问题,并允许您更轻松地控制用户必须选择的选项,因为您可以根据数据库轻松构建阵列或手动构建阵列以确保所有代码完全是每个复选框都相同。

$possibleValues = [
    "Ahri",
    "Aatrox",
    "xx1",
    "xx2",
];

foreach($possibleValues as $value) {
    echo "<td>{$value}<input type='checkbox' name='champ[]' value='{$value}'/></td>";
}

侧注#2

一些明智的代码缩进是个好主意。 它可以帮助我们阅读代码,更重要的是它可以帮助调试代码Take a quick look at a coding standard为了您自己的利益。 您可能会被要求在几周/几个月内修改此代码,最后您会感谢我。

正如您在上面提供的代码中所看到的(在HTML PHP中),每行都有一定的间距,使代码更具可读性。将具有多个子元素的元素分解为多行(大多数时间)也是有益的,这使得它更易读,更易于使用。