lundi 5 juin 2017

Why check boxes in form giving Undefined index: error

I am trying to pass values using checkbox in form.In my code if checkbox is checked then values is passing.But if none of the check boxes are checked then it will give the Undefined index: error. how to fix this one? (I am passing value in same page.)

<form name="listdata" action="" method="POST"> 
    <?php

//$uname=$_SESSION['username'];

//$uid=$mysqli->query()


// get the records from the database
    if ($result = $mysqli->query("SELECT * FROM payment WHERE Approved='0'"))
    {
        // display records if there are records to display
        if ($result->num_rows > 0)
        {
            // display records in a table
            echo "<table class='table table-hover'>";

            // set table headers
            echo "<tr><th>Payment ID</th><th>User ID</th><th>Name</th><th>NIC</th><th>Amount</th><th>Pay date</th><th>Approval</th></tr>";

            while ($row = $result->fetch_object())
            {
                // set up a row for each record
                echo "<tr>";
                echo "<td >" . $row->PayID . "</td>";
                echo "<td>" . $row->UID . "</td>";
                echo "<td>" . $row->Name . "</td>";
                echo "<td>" . $row->NIC . "</td>";
                echo "<td>" . $row->Amount . "</td>";
                echo "<td>" . $row->PayDate . "</td>";
                echo "<td>" ?> <input type="checkbox" name="num[]" value="<?php echo $row->PayID; ?>" /> <?php echo "</td>";
                echo "</tr>";
            }

            echo "</table>";
        }
        // if there are no records in the database, display an alert message
        else
        {
        echo "No Pending payment to display!<br />";
        }
    }
    // show an error if there is an issue with the database query
    else
    {
        echo "Error: " . $mysqli->error;
    }

// close database connection
$mysqli->close();

?>
    <input type="submit" class="btn btn-primary btn-lg " value="APPROVE" name="submit" />
    <a href="report.php" class="btn btn-primary btn-lg " >REPORT</a>
    <a href='logout.php' class="btn btn-primary btn-lg ">LOG OUT</a>
    </form>



<?php 


if(isset($_POST["submit"]))
{
    if(empty($_POST['num']))
    {
        echo "Error";
    }else{
        $box=$_POST['num'];
    While(list($key,$val)=@each($box)){
        echo "$val";
    }
    }


}
?>




Aucun commentaire:

Enregistrer un commentaire