单选按钮值未存储在数据库中

单选按钮值未存储在数据库中

问题描述:

I created a radio button in which when user click on yes value then text box is shown to user otherwise they remain hide.
My question when user select yes then I am able to store yes value in sql but when user click on no value that value is not stored in datbase

<script>
$(document).ready(function(){
  $(function() {
     $('input[name="yeshave"]').on('click', function() {
        if ($(this).val() == 'yes') {
           $('#textboxes').show();
        } else {
           $('#textboxes').hide();
        }
     });
   });
});
</script>

no radio button code:

<table>
   <tr>
      <td>Do you haver passport:</td>
      <td><input type="radio" name="yeshave" value="yes"> Yes</td>
      <td><input type="radio" name="yeshave" value="no"> No</td>
   </tr>
</table>

SQL code is:

if(isset($_POST['yeshave']))
{
    $selectedValue=$_POST['yeshave'];
}



 $query = "INSERT INTO upload (name, size, type, image,passport_no,dateofissue,dateofexpiry,placeofissue, having_passport ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$image','".$passno."','".$doi."','".$doe."','".$poi."','".$selectedValue."')";

mysqli_query($conn,$query) or die('Error, query failed');

Set check one of them. Try with -

$selectedValue = isset($_POST['yeshave']) ? $_POST['yeshave'] : 'no';

JavaScript:

$(document).ready(function() {
    $('input[name="yeshave"]').on('click', function() {
        $('#textboxes').toggle($(this).val() == 'yes');
    });
});

In PHP:

$selectedValue = $_POST['yeshave'] ? $_POST['yeshave'] : 'no';
  1. You don't need $(function() inside ready
  2. Use toggle to hide/show #textboxes
  3. In PHP, check if value is present, if not use default no