选中复选框?

时间:2013-02-25 02:07:04

标签: php html checkbox isset checked

现在我已经做了很多研究,在PHP中尝试了很多方法,包括$ _POST isset .. 的foreach  等

但我需要一些帮助!

基本上,只想检查是否已选中复选框。然后,如果选中此复选框,则为$次访问次数加1。

只要我可以检查复选框是否已选中,我想我可以从那里弄明白!

提前致谢

(注意:$ visits是访问一个属性的次数。此编码显示从文件中读取的属性信息)

<?
    $filename = "house.txt";
    $filepointer = fopen($filename,"r");  // open for read
?>

<html>
<head>
    <h1> Search for properties</h1>
    <form method = "post" action= "visit.php">
        Enter max price
        <input type = "text" name = "max" value="<?=$Max;?>">
        <input type = "submit" name = "submit">
        <br><i><p>Properties found</p></i></br>
    </form>
</head>
</html>

<?
    $myarray = file ($filename);
    for ($mycount = 0; $mycount < count($myarray); $mycount++ ) { // one input line at a time
        $aline = $myarray[$mycount];
        $postcode = getvalue($aline,0);
        $value = getvalue($aline,1);
        $image = getvalue ($aline,2);
        $visits = getvalue($aline,3);
        $Max = $_POST['max'];

        if ($value < $Max) {
            print "<table border = 2>";
            print "<FORM METHOD='POST' ACTION='visit.php' >";
            print "<td> <input type='checkbox' name='check' value='Yes' > $postcode </td><BR> \n";
            print "<td>$value <BR>";
            print "<td>$image<BR>";
            print "<td>$visits<BR><p>";
            print "</table>";
            print "</form>";
        }
    }

    function getvalue ($aline, $commaToLookFor) {   
        $intoarray = explode(",",$aline);
        return  $intoarray[ $commaToLookFor];  
    }

    if (isset($_POST['check']) && $_POST['check'] == 'Yes') {
        echo "checked!";
    } else {
        echo "not checked!.";
    }
?>

1 个答案:

答案 0 :(得分:1)

您提交的表单与您认为的表单不同...您在页面上有两个表单,都提交到&#34; visit.php&#34;。这条线不应该存在:

print "<FORM METHOD='POST' ACTION='visit.php' >";

...因为您已经在文件顶部创建了表单。

这需要对您的代码进行一些重组,但基本的想法是您只需要一个包含所有字段和提交按钮的表单,否则您将提交包含最高价格的表单没有别的。

或者,如果您确实需要单独的表单(我对您的用例不够熟悉),那么第二个表单将需要自己的提交按钮。

简化工作代码:

 print "<table border = 2>";
print "<FORM METHOD='POST' ACTION='visit.php' >";
  print "<td> <input type='checkbox' name='check' value='Yes' > $postcode </td><BR> \n";
  print "<td> <button type='submit'>Submit</button> </td><BR> \n";
 print "</table>";
 print "</form>";

//personally I would just check for isset($_POST['check']), but it doesn't really matter... 
if (isset($_POST['check']) && $_POST['check'] == 'Yes') 
{
  echo "checked!";
}
else
{
  echo "not checked!.";
}
相关问题