用php显示复选框的多个字段

时间:2016-05-16 14:42:28

标签: php html

我有一个包含不同复选框字段的表单。但是,我能够使用foreach循环显示它们的值,但是想知道是否可以通过它们的选择方式显示它们。下面是我的代码。

<html>
<head></head>
<body>
<form action="states.php" method="post">
    Draft<input type="checkbox" name="states[]" id="design_states" value="Draft">
    Design<input type="checkbox" name="states[]" id="design_states" value="design">
    Review<input type="checkbox" name="states[]" id="design_states" value="Review">
    Approved<input type="checkbox" name="states[]" id="design_states" value="Approved">
    Issued For Construction<input type="checkbox" name="states[]" id="color" value="Issued For Construction">                      
    <input type="submit" value="submit">
</form>
<body>
</html>

<?php

    if(isset($_POST['states'])) {
      $name = $_POST['states'];

      echo "You chose the following workflow: <br>";
      foreach ($name as $states){
        echo $states."<br />";
      }} // end brace for if(isset

    else {

      echo "You did not choose a workflow.";

    }
?>

如果用户选择首先发布施工,然后选择草稿,我希望将这些值显示为先取选而不是草稿然后发布用于施工。

1 个答案:

答案 0 :(得分:0)

echo "You chose the following workflow: <br>";
foreach (array_intersect($states, $name) as $state){
    echo $state."<br />";
}

echo "You don't chose the following workflow: <br>";
foreach (array_diff($states, $name) as $state){
    echo $state."<br />";
}