Tuesday, March 23, 2021

How to use checkbox in PHP

 In this tutorial I will show you how to declare and use checkbox using PHP programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Program Listing

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Player Sports Tournament Using Checkbox in PHP</title>
    <style>
        fieldset {
            display: inline-block;
            padding: 10px 30px 20px;
        }
    </style>
</head>
<body>
    <form action="result.php" method="POST">
        <fieldset>
            <h3>Player Sports Tournament Using Checkbox in PHP</h3>
            <label for="fname">Name:</label>
            <input type="text" id="name" name="name" required><br>
            
            <p>Sports Choices:</p>
            <input type="checkbox" name="sports[]" value="Football" id="Football" checked><label for="Football">Football</label><br>
            <input type="checkbox" name="sports[]" value="Basketball" id="Basketball"><label for="Basketball">Basketball</label><br>
            <input type="checkbox" name="sports[]" value="Handball" id="Handball"><label for="Handball">Handball</label><br><br>

            <button type="submit">Submit</button>
        </fieldset>
    </form>
</body>
</html>


result.php


<?php
    $name = $_POST["name"];
    $list = "";
    foreach($_POST['sports'] as $value){
        $list .= "<label>".$value."</label><br>";
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Player Sports Tournament</title>
    <style>
        fieldset {
            display: inline-block;
            padding: 10px 30px 20px;
        }
    </style>
</head>
<body>
    <fieldset>
        <h3>Player Sports Tournament</h3>
        <label>Name: <?php echo $name; ?></label><br>
        <label>Sports Choices:</label>
        <?php echo $list; ?>
        <br>
        <a href="index.php">Back</a>
    </fieldset>
</body>
</html>

No comments:

Post a Comment