必须提交两次

时间:2014-03-16 07:30:19

标签: php html arrays forms session-variables

好的我是编程的新手,我遇到了一些复选框的问题 如果选中复选框,我想存储,并在按下按钮时显示 我正在进行过滤搜索,因此当一个人选中一个复选框时,它只显示该过滤器 但是,只有在我提交并选择或取消选中两次复选框时,它才有效 我怎样才能让它只需要做一次呢?

<form name="search" action="<? $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="checkbox" name="checkAllMyCB" id="checkAllMyCB" onclick="jqCheckAll2( this.id, 'myCB' )"/>All
   <input name="category[]" type="checkbox" <? if (in_array("art", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="art">Art
   <input name="category[]" type="checkbox" <? if (in_array("church", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="church">Church
   <input name="category[]" type="checkbox" <? if (in_array("education", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="education">Education/Schools
   <input name="category[]" type="checkbox" <? if (in_array("food", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="food">Food
   <input name="category[]" type="checkbox" <? if (in_array("gardening", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="gardening">Gardening
   <input name="category[]" type="checkbox" <? if (in_array("kids", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="kids">Kids
   <input name="category[]" type="checkbox" <? if (in_array("music", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="music">Music
   <input name="category[]" type="checkbox" <? if (in_array("outdoors", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="outdoors">Outdoors
   <input name="category[]" type="checkbox" <? if (in_array("seniors", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="seniors">Seniors
   <input name="category[]" type="checkbox" <? if (in_array("sports", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="sports">Sports/Physical Activity
   <input name="category[]" type="checkbox" <? if (in_array("support", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="support">Club/Support Group
   <input type="submit" name="Go2" id="Go2" value="Go &gt;" />
<? 
$_SESSION['category'] = $_POST['category']; ?>

</form>

1 个答案:

答案 0 :(得分:1)

您的代码中有两个错误:

  1. 您首先显示表单,然后将值设置为SESSION。这应该是相反的方式,因为在提交时,您的表单将首先显示,并且仅在值设置之后显示,这就是为什么您有两次提交它以查看值的问题。

  2. 您没有检查SESSION变量是否已在in_array之前设置,这最初会引发错误。与$_POST['category']

  3. 相同的情况

    首先将其放在首位:

    <?
    if (isset($_POST['category'])){
    $_SESSION['category'] = $_POST['category']; }
    else
    {
        unset($_SESSION['category']);
    }
    ?>
    

    然后将您的表单代码更改为:

    <form name="search" action="" method="post">
       <input type="checkbox" name="checkAllMyCB" id="checkAllMyCB" onclick="jqCheckAll2( this.id, 'myCB' )"/>All
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("art", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="art">Art
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("church", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="church">Church
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("education", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="education">Education/Schools
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("food", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="food">Food
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("gardening", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="gardening">Gardening
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("kids", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="kids">Kids
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("music", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="music">Music
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("outdoors", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="outdoors">Outdoors
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("seniors", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="seniors">Seniors
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("sports", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="sports">Sports/Physical Activity
       <input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("support", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="support">Club/Support Group
       <input type="submit" name="Go2" id="Go2" value="Go &gt;" />
    </form>