How to store HTML checkbox values and radio button values in database using PHP
Posted: Fri Mar 30, 2018 8:25 am
- <?php
- $conn=mysqli_connect("localhost","root","tssfl");
- mysqli_select_db($conn,"simple");
- if(isset($_POST['btn'])) {
- if (isset($_POST['checks'])){
- $terms="Accepted";
- }elseif (!isset($_POST['checks'])){
- echo "Accept terms and Condition";
- exit();
- }
- if (isset($_POST['gender'])) {
- $gvalue=$_POST['gender'];
- }elseif (!isset($_POST['gender'])) {
- echo "Select gender";
- exit();
- }
- $sql = "INSERT INTO checks (sex,terms) VALUES ('$gvalue','$terms')";
- if (mysqli_query($conn, $sql)) {
- echo "success";
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Testing</title>
- </head>
- <body>
- <div style="margin-left: auto;margin-right: auto; width:50%;background-color: #eaeaea;margin-top: 100px;">
- <h1>Select user information</h1>
- <form action="index.php" method="post">
- <p><input type="checkbox" value="Accepted" name="checks">Terms and Condition</p>
- <p>Choose Gender</p>
- <p><input type="radio" value="Male" name="gender">Male</p>
- <p><input type="radio" value="Female" name="gender">Female</p>
- <p><input type="radio" value="Other" name="gender">Other</p>
- <input type="submit" name="btn" value="Save">
- </form>
- </div>
- </body>
- </html>