John Davidson

how to add default value to uncheck checkboxes in html form and pass it to MySQL database using php

0 comments
Message:


I want to create columns for each and every checkboxes values in MySQL database. I want to pass 'YES' to checked checkboxes and 'NO' for the uncheck check boxes. when I use following code and submit it MySQL database using php, still uncheck checkboxes does not show 'NO' value in MySQL database.


<input type="hidden" value="NO" name="MBBS" >
<input type="checkbox" value="YES" name="MBBS" >
<label for="MBBS">MBBS</label><br>

<input type="hidden" value="NO" name="MD" >
<input type="checkbox" value="YES" name="MD" >
<label for="MD">M.D</label><br>


My Answer:
To add default values to unchecked checkboxes in an HTML form and pass it to a MySQL database using PHP, you can follow these steps:

1. In your HTML form, make sure to set a hidden input field with the same name as the checkbox and a default value of 'NO'. This hidden input field will be submitted along with the form data when the checkbox is unchecked.

html




2. In your PHP script that processes the form data, check if the checkbox is checked. If it is checked, set the value to 'YES'. If it is not checked, the hidden input field with the default value of 'NO' will be submitted.

php
$checkbox_value = isset($_POST['checkbox_name']) ? 'YES' : 'NO';


3. Insert or update the checkbox value in your MySQL database table.

php
// Assuming you have a connection to your MySQL database
$sql = "INSERT INTO your_table_name (checkbox_column_name) VALUES ('$checkbox_value')";
// Execute the SQL query


By following these steps, you should be able to add default values to unchecked checkboxes in your HTML form and pass it to a MySQL database using PHP.

Rate this post

4 of 5 based on 3288 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us