How to store HTML checkbox values and radio button values in database using PHP

Post Reply
cache
Member
Reactions: 12
Posts: 22
Joined: 6 years ago

#1

  1. <?php
  2. $conn=mysqli_connect("localhost","root","tssfl");
  3. mysqli_select_db($conn,"simple");
  4. if(isset($_POST['btn'])) {
  5.     if (isset($_POST['checks'])){
  6.         $terms="Accepted";
  7.     }elseif (!isset($_POST['checks'])){
  8.         echo "Accept terms and Condition";
  9.         exit();
  10.     }
  11.   if (isset($_POST['gender'])) {
  12.     $gvalue=$_POST['gender'];
  13.   }elseif (!isset($_POST['gender'])) {
  14.     echo "Select gender";
  15.     exit();
  16.   }
  17. $sql = "INSERT INTO checks (sex,terms) VALUES ('$gvalue','$terms')";
  18. if (mysqli_query($conn, $sql)) {
  19.         echo "success";
  20.     }  
  21. }
  22.  
  23. ?>
  24. <!DOCTYPE html>
  25. <html>
  26. <head>
  27.     <title>Testing</title>
  28. </head>
  29. <body>
  30.     <div style="margin-left: auto;margin-right: auto; width:50%;background-color: #eaeaea;margin-top: 100px;">
  31.         <h1>Select user information</h1>
  32. <form action="index.php" method="post">
  33.     <p><input type="checkbox" value="Accepted" name="checks">Terms and Condition</p>
  34.     <p>Choose Gender</p>
  35.     <p><input type="radio" value="Male" name="gender">Male</p>
  36.     <p><input type="radio" value="Female" name="gender">Female</p>
  37.     <p><input type="radio" value="Other" name="gender">Other</p>
  38.     <input type="submit" name="btn" value="Save">
  39. </form>
  40.     </div>
  41. </body>
  42. </html>

0
Post Reply

Return to “Software Engineering”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests