How to save the Insert Radio Button with Bootstrap to the Mysql database

Asked

Viewed 107 times

1

I am developing a registration form but I had to include a field (Radio Button) in my project. However, I am unable to save the Radio Button Insert to my Mysql database.

The code below is in bootstrap pattern, but the variable name which was used to define the fields that would be saved named database, is already being used by the flexRadioDefault format. This way, I cannot use it to perform the insert in the bank, as was done in the other fields of the form.

Example:

<!-- FORMULÁRIO -->

<div class="col-sm">
  <label for="cep">Cep</label>
  <input type="text" class="form-control" placeholder="CEP" name="cep">
</div>

<div class="form-group col-sm-3">

  <label for="cargo">Cargo</label>
  <input type="text" class="form-control" placeholder="Digite seu Cargo" name="cargo">

</div>

</div>

<div class="form-group">
  <div class="form-check">
    <input class="form-check-input" type="radio" name="flexRadioDefault">
    <label for="flexRadioDefault1">Homem</label>
  </div>

  <div class="form-check">
    <input class="form-check-input" type="radio" name="flexRadioDefault">
    <label for="flexCheckDefault2">Mulher</label>
  </div>
</div>
// INSERT BANCO

include "conexao.php";

$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$endereco = $_POST['endereco'];
$bairro = $_POST['bairro'];
$estado = $_POST['estado'];
$cep = $_POST['cep'];
$cargo = $_POST['cargo'];
$sexo = $_POST['sexo'];

$sql = "INSERT INTO `formulario`(`nome`, `telefone`, `email1`, `email2`, `endereco`, `bairro`, `estado`, `cep`, `cargo`, `sexo`,) VALUES
('$nome','$telefone','$email1','$email2','$endereco','$bairro','$estado','$cep','$cargo','$sexo')";
  • Oi Caio. Add the snippet of PHP code where you try to save the contents of this form in your database.

  • I tried to put code under it the same way, but it didn’t work..

2 answers

0


based on the example of Bootstrap you don’t need to use the same name and id that they used, just the same class form-check-input and form-check-input which is where the stylings are. The rest you adapt to your case. Which after looking at your PHP code, I believe is something like this:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="form-check">
  <input class="form-check-input" type="radio" name="sexo" value="Masculino" id="radio-sexo-masculino">
  <label class="form-check-label" for="radio-sexo-masculino">
    Masculino
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="sexo" value="Feminino" id="radio-sexo-feminino">
  <label class="form-check-label" for="radio-sexo-feminino">
    Feminino
  </label>
</div>

so the content should be within $_POST['sexo'].

I suggest using Male and Female for sex options instead of Male and Female source.

And also research a little bit about SQL Injection, because it seems that your system is open to this, and you have to treat these values before doing your query

  • The problem when I change the variable name= "flexRadioDefault" to name = "sex", Radiobutton is already marked.

  • Strange. what makes the radio to be marked in the example above is the attribute checked removing it, both will be unchecked by default

  • In fact it is even unchecked. But when I mark one and then the other, it does not alternate the marking, marking both at once.

  • The attribute was missing value above. agr o $_POST['sexo'] should come empty or 'Male' or 'Female'. Try copying the snippet to your code and see if the two fields are still marked.

  • That’s right. I don’t know how to thank you. Thank you very much!!!

  • Not at all @Caiofilipe . Consider accepting the answer as a resolution to your question!

Show 1 more comment

0

I believe your problem is because you need to put a value attribute with the value you want to insert from your input, example:

<input class="form-check-input" type="radio" value="M" name="flexRadioDefault">

<input class="form-check-input" type="radio" value="F" name="flexRadioDefault">

In php:

$gender = $_POST['flexRadioDefault'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.