Amy Amy - 22 days ago 6
PHP Question

Checkbox resetting after button click, and giving undefined index notice in PHP

I have the below code:

<div class="ym-form">
<div class="ym-fbox">
<div class="ym-grid linearize-level-2">
<div class="ym-g50 ym-gl">
<div class="ym-gbox">

<table>
<tbody>
<tr>
<form action="" form method="post">
<input type="checkbox" name="Finished" value="Finished">Finished
<input type="checkbox" name="Unfinished" value="Unfinished">Unfinished
<input type="checkbox" name="Stalled" value="Stalled">Stalled
<input type="checkbox" name="All" value="All">All
</form>

<br>
<br>





<?php
ini_set('display_errors',"1");

$select = '<select multiple>';

$lines = file('project-list.txt');
$fifth_column = array();
foreach ($lines as $line){
$parts = preg_split('/\s+/', $line);
$count = 0;
foreach ($parts as $partVal){
if ((in_array($partVal, $fifth_column) == FALSE) && $count == 4){
$fifth_column[] = $partVal;
}
$count++;
}
}

foreach($fifth_column as $value){
$select .= "<option value='".$value."'>".$value."</option>";
}

$select .= '</select>';

echo $select;


?>

<form method="post">
<br>

Dates:

<label for="from">From:</label>
<input id="from" name="from" type="date" value="2015-05-25"/>

</div>


<label for="to">To:</label>
<input id="to" name="to" type="date" value="2015-06-24"/>
</div>
</div>
</div>
<div class="ym-grid">
<div class="ym-gbox">
<input type="submit" class="ym-button ym-small" name = "Apply" style="margin-top:1em;max-width:11em" value="Apply" />
</div>
</div>
</div>
</div>
</form>
<BR>
<form method="post">
<label for="search">Search</label>
<INPUT TYPE = "Text" VALUE ="" NAME = "search">
<input type="submit" class="ym-button ym-small" name = "Apply1" style="margin-top:1em;max-width:11em" value="Apply" />

</form>
<br>
<br>

<?php
if($_POST){
if(isset($_POST['Apply']) && (isset($_POST['Finished']) && $_POST['Finished'] == 'Finished')) {
Apply();
}
}
function Apply() {
$searchthis = "finished";

$matches = array();

$handle = @fopen("project-list.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
if(strpos($buffer, $searchthis) !== FALSE)
$matches[] = $buffer;
}
fclose($handle);
}
foreach($matches as $child) {
echo "<table border=\"5\" cellpadding=\"10\">";
echo "<tr>";
echo $child . "<br />";
}
echo "</tr>";
echo "</table>";
}
?>








</body>
</html>


However, when the button is clicked, with the 'Finished' checkbox being selected I get the error:
Notice: Undefined index: Finished in C:\projectlist\test1.php on line 511

How can I get it to see the checkbox has been checked, once the button is clicked?

Thanks!

Answer

You need to check the finished input is set and check for its value, it will be unset if not checked:

<?php
if($_POST){
    if(isset($_POST['Apply']) && (isset($_POST['Finished']) && $_POST['Finished'] == 'Finished')) {
        Apply();
    }
}
function Apply() {
    $searchthis = "finished";

    $matches = array();

    $handle = @fopen("project-list.txt", "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle);
            if(strpos($buffer, $searchthis) !== FALSE)
                $matches[] = $buffer;
        }
        fclose($handle);
    }
    foreach($matches as $child) { 
        echo "<table border=\"5\" cellpadding=\"10\">";
        echo "<tr>";
        echo $child . "<br />";
    }
    echo "</tr>";
    echo "</table>";
}
?>
<form action="" method="post">
    <input type="checkbox" name="Finished" value="Finished">Finished 
    <input type="checkbox" name="Unfinished" value="Unfinished">Unfinished
    <input type="checkbox" name="Stalled" value="Stalled">Stalled
    <input type="checkbox" name="All" value="All">All
    <input type="submit" class="ym-button ym-small" name = "Apply" style="margin-top:1em;max-width:11em" value="Apply" />
</form>

You also had extra { } (brackets) in your code.

Good to see your making progress on your script, as i had seen your post the other day.