0

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

但我需要一些帮助!

基本上,只想检查复选框是否已被选中。然后,如果复选框已被选中,则将 $visits 的数量加 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!.";
    }
?>
4

1 回答 1

1

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

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!.";
}
于 2013-02-25T02:39:27.867 回答