-1

Hi I have multiple check boxes name make and female I want that if a person select make and don't select female it input nothing or false in database now if I don't select example: female it return nothing in female row but it also get a error

Undefined index: Female in /storage/sdcard1/www/3/signup/index.php on line 11

but I don't want this error and I want the if female not select it insert false and don't show error I don't want the selected check box value to be true because it is wrote as on in database

Code index.php

<?php 
if(isset($_POST['submit'])){
    
$conn = new PDO("sqlite: sign.db");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $Email = $_POST['Email'];
    $First = $_POST['First'];
    $Last = $_POST['Last'];
    $Password = $_POST['Password'];
    $Male = $_POST['Male'];
    $Female =$_POST['Female'];
     $Dateofb = $_POST['Dateofb'];
  require_once 'imp.php';
    
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css"  >
<script src="index.js" ></script>
<title>Survey</title>
</head>
<body>
<form action="" method="post"  >
<div class="container" >
<div class="form" >

<input type="email" class="first"  id="Email" name="Email" placeholder="Email" required="required">
<input type="text" class="second"  id="First" name="First" placeholder="First name" required="required">
<input type="text" class="last"  id="Last" name="Last" placeholder="Last name" required="required">
<input type="password" class="pass" name="Password" id="Password" placeholder="Password" required="required">

<div class="day" >

<p class="bd" >Birthday Date:</p>

<input type="date" class="date" id="Dateofb" name="Dateofb" >

</div>
<div>
<div class="malee" >
<input type="checkbox" class="male" id="Male" name="Male">
<p class="mal" >Male</p>
</div>
<div class="femalee" >
<input type="checkbox" class="female" id="Female" name="Female" >
<p class="fem" >Female</p>
</div>
</div>
<div >
<input class="submit" id="submit" type="submit" name="submit"  >
</div>
<div class="acc" >
already have account <a href="#" >Login</a>
</div></div>
</div>
</form>
</body>
</html>
        

Code imp.php

<?php

    
$sql = "INSERT INTO signup (`Email`, `First`, `Last`, `Password`, `Male`, `Female`, `Dateofb`) VALUES ('$Email', '$First', '$Last', '$Password', '$Male', '$Female', '$Dateofb');";
 
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    $conn->connection = null;
?>
E_net4
  • 27,810
  • 13
  • 101
  • 139
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – El_Vanja May 17 '21 at 10:28
  • Only checked boxes are submitted. You need to check if a box's name is set in `$_POST` and manually assign a value you wish to use instead. – El_Vanja May 17 '21 at 10:29
  • Ok I will try to assign a value but what if I don't select female will the value set on female still get input – Ch Reyyan Gujjar May 17 '21 at 10:34
  • Unchecked boxes simply won't be present in `$_POST`. Therefore, you need to check all your checkboxes with `isset`. If they're not set, then you can give your variable some default value you wish (you mentioned `false`). – El_Vanja May 17 '21 at 10:58

1 Answers1

-1

The problem is that only checked boxes are submitted. You need to check if a box's name is set in $_POST and IF NOT: manually assign a value you wish to use instead

I.e. use this short hand if/else

$Female = (isset ($_POST['Female'])) ? 1 : 0;
Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19