0

I have an error on line 25 & 26. It says 'Invalid argument supplied for foreach()' I want to echo the selected event from checkbox. But at the display page. it doesn't appear (echo). However, it is available in the database.

I put comment on the section I feel there is a mistake

data.php

<html>

<head>
    <title>data.php</title>
</head>
<center>
    <h1>
        <p style="color:red;">Your DATA</p>
    </h1>

    <body style="background-color:powderblue;">
        <center>
            <h2>
                <?php
                $Firstname = $_POST['f_name'];
                $Lastname = $_POST['l_name'];
                $emeil = $_POST['emeil'];
                $friend = $_POST['friend'];
                $event = $_POST['check_list'];
                $selected = $event; //trying to define variable
                if (is_array($event)) {
                    foreach ($_POST['check_list'] as $selected) { //trying to echo multiple data from checkbox
                        echo $selected;
                    }
                }
                echo " 1)Firstname :   " . $Firstname;
                echo "<br>";
                echo " 2)Lastname :   " . $Lastname;
                echo "<br>";
                echo " 3)emeil :   " . $emeil;
                echo "<br>";
                echo " 4)friend :   " . $friend;
                echo "<br>";
                echo " 5)event : " . $selected; //echo statement
                echo "<br>";
                $link = mysqli_connect("localhost", "root", "", "eventreg");
                if ($link === false) {
                    die("ERROR:Could not connect." .
                        mysqli_connect_error());
                }
                $sql = "INSERT INTO eventreg (f_name,l_name,email,event,friend) VALUES ('$Firstname','$Lastname','$emeil','$event','$friend')";
                if (mysqli_query($link, $sql)) {
                    echo "Records inserted successfully.";
                } else {
                    echo "ERROR:Could not able to execute" .
                        mysqli_error($link);
                }
                mysqli_close($link);
                ?>
        </center>
        </h2>
    </body>

</html>

This the coding for the registration page (checkbox)

register.php

<html>

<head>
    <h1>Online Event Registration</hi>
        <h2> Register today</h2>
</head>

<body style="background-color:powderblue;">
    <form action="data.php" method="post">
        First name :<input type="text" name="f_name" required>
        <br>
        <br>
        Last name :<input type="text" name="l_name" required>
        <br>
        <br>
        emeil :<input type="text" name="emeil" size="20" maxlength="60" required /></p>
        <br>
        How many friend you will bring along?
        <br>
        <br>
        <select id="friend" name="friend">
            <option value="one">ONE</OPTION>
            <option value="two">TWO</OPTION>
            <option value="three">THREE</OPTION>
            <option value="four">FOUR</OPTION>
        </select>
        <br>
        <br>
        <br>
        How do you hear about the event ? //checkbox coding
        <br>
        <input type="checkbox" id="cfriend@colleague" name="check_list" value="friend@colleague">
        <label for="cfriend@colleague"> Friend @ Colleague</label><br>
        <input type="checkbox" id="cadvertisement" name="check_list" value="advertisement">
        <label for="cadvertisement"> Advertisement</label><br>
        <input type="checkbox" id="cTV" name="check_list" value="TV">
        <label for="cTV"> TV</label><br>
        <br>
        <br>
        <input type="submit" value="Register">
    </form>
    <a href="http://localhost:8080/final%20project/paparan.php">paparan Admin</a>
</body>

</html>

I have referred to a few examples online for correct foreach usage but I still couldn't figure out what is the mistake. I believe it relates to the checkbox coding in registration page coding (the second coding part)

Danz
  • 252
  • 1
  • 8
aymandinie
  • 13
  • 4

1 Answers1

0

change name of checkbox to array check_list[] like this

<input type="checkbox" id="cfriend@colleague" name="check_list[]" value="friend@colleague">
<label for="cfriend@colleague"> Friend @ Colleague</label><br>
<input type="checkbox" id="cadvertisement" name="check_list[]" value="advertisement">
<label for="cadvertisement"> Advertisement</label><br>
<input type="checkbox" id="cTV" name="check_list[]" value="TV">
<label for="cTV"> TV</label>

to get array data

<?php
$Firstname = $_POST['f_name'];
$Lastname = $_POST['l_name'];
$emeil = $_POST['emeil'];
$friend = $_POST['friend'];
$event = implode(", ", $_POST['check_list']);
echo " 1)Firstname :   " . $Firstname;
echo "<br>";
echo " 2)Lastname :   " . $Lastname;
echo "<br>";
echo " 3)emeil :   " . $emeil;
echo "<br>";
echo " 4)friend :   " . $friend;
echo "<br>";
echo " 5)event : " . $event;
echo "<br>";
Danz
  • 252
  • 1
  • 8