php

PHP program to get the values of checkboxes on Form Submit

When creating a form in PHP we used multiple input controls. If you are using checkbox input in your form and want to get the values of selected checkboxes on the Form Submit then you can the code examples explained in this post.

We can design and create our Form using HTML code and using PHP code we can access the values of selected checkboxes and can use them to save to database or any other process on the server.

Let's create our form first. You can use the below code example to create your form that will have checkbox type controls.

<form action="save_brand.php" method="post">
    TATA<input type="checkbox" name="car_brand[]" value="tata">
    Hyundai<input type="checkbox" name="car_brand[]" value="hyundai">
    Volvo<input type="checkbox" name="car_brand[]" value="volvo">
    Tesla<input type="checkbox" name="car_brand[]" value="tesla">

    <input type="submit" value="submit">
</form>

The PHP code that will run after submitting the form and getting the values of selected checkboxes is as below.

save_brand.php

<?php
    $car_brands = $_POST['car_brand'];

    foreach ($car_brands as $brand){ 
        echo $brand;
    }
?>
Was this helpful?