Shazia E
Shazia E

Reputation: 3

Check boxes checked?

Now I've done a lot of research, tried a lot of methods in PHP, including $_POST isset.. foreach etc

But i need some help!

Basically, just want to check if the checkboxes have been checked. And THEN, add 1 to the number of $visits made if the checkbox has been checked.

As long as I can check if the checkbox is checked, i think i can figure it out from there!

Thanks in advance

( note: $visits is the number of times a property has been visited. This coding displays property information read from a file)

<?
    $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!.";
    }
?>

Upvotes: 0

Views: 157

Answers (1)

Matt Browne
Matt Browne

Reputation: 12419

You're submitting a different form than the one you think you are...you have two forms on the page, both submitting to "visit.php". This line shouldn't exist:

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

...since you've already created the form at the top of your file.

This will require a little reorganization of your code, but the basic idea is that you want one and only one form that contains all the fields and the submit button, otherwise you're submitting the form that contains the max price and nothing else.

Alternatively, if you actually do need separate forms (I'm not familiar enough with your use case to be sure), then the second form would need its own submit button.

Simplified working code:

 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!.";
}

Upvotes: 1

Related Questions