如何排序(优先级)从编号复选框形式的值在PHP


How to rank (prioritize) values from numbered checkbox form in PHP

我使用下面的代码让用户对他们更熟悉的编程语言进行排名。用户需要从1-3进行排名(1是他们最舒服的)

<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP     <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>

一旦用户对编程语言进行了优先级排序并点击提交,我如何在下一页回应排名选择?(例如,你的第一选择是x,第二选择是y,第三选择是z)

我将这样做(注意,我已经更改了表单元素的name属性的值):

<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP     <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>

在php中:

//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
   //echo out here first, second, and third rank with each iteration respectively.
}

sort函数仅按值对数组进行排序,同时保持键关联。

我不确定标签输入类型="number"是否存在。

你做得更好

<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
 <legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>

你不能在单选标签或复选框标签中使用'required'属性

所以你写了一个检查javascript函数是否选中了单选框

<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
  if($("input[name=php]:checked").val() =="")
  return false;
...
 return true;
}
</script>

或者

<input type="text" name="php">

那么在下一页你可以这样做

$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..