Search code snippets, questions, articles...

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.
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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;
    }
?>
<?php
    $values = $_POST['checbox_name'];

    foreach ($values as $val){ 
        echo $val;
    }
?>
Was this helpful?
0 Comments