Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
Radio buttons to real-time interact with the database

I would like to get some help on my coding, I am struggling to get the radio buttons connecting to MySQL database.

What it is that I want is so that when someone clicks either 'yes' or 'no' on the radio buttons it automatically stores in the MySQL database, I have had a look at various other ways that I can do it but I can't get it to work. Below is a screenshot.

image here

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If I understand you correctly, You want to store the radio value in MySQL. There's no such thing as "store it in PMA". You can store the value in MySQL, but you can't without using php. (Your current code is only HTML)

With PHP & MySQL, you could do something like this:

$sql = 'INSERT INTO mytablename(checkbox_value) VALUES("' . $_POST['gen1'] . '")';

$_POST['gen1'] could return the selected value (e.g.: "yes"), and checkbox_value could be enum and only contain "yes" and "no" values.

If you're looking to insert these values into MySQL, Consider learning about INSERT INTO in MySQL Documentation. Also, learn about $_POST HTTP variable.

over 4 years ago · Santiago Trujillo Relatório

0

if you wanna make a real-time interaction with the database you need to more than just HTML, in fact, you need 2 extra languages to do that, so lets us begin.


The HTML

First of all, you have your HTML5 form it could e.g look like this:

<form>
    <input type="radio" name="sex" value="0"> Man
    <input type="radio" name="sex" value="1"> Women
</form>

important thing about this! The <form> we don't give it a method after some we don't need it when using jQuery/Ajax to communicate with the backend


Real-time interact (by using Ajax)

include the jQuery library and link to it with <script src="jQuery.min.js"></script>

Right before your </body> tag includes this code, it looks for if any radio bottoms have changed and if that true then it looks for the value of input named "sex" and store it in a variable called value.

We send the value as a POST named sex, so in db.php it will look like a $_POST['sex']

<script>
    $(":radio").change(function() {
        var value = $('input[name=sex]:checked').val();

        $.ajax({  
            type: 'POST',  
            url: 'db.php', 
            data: { 
                sex: value 
            }
        });
    });
</script>

The PHP (backend)

Now we just need to grab the data and run a query to get it into the database.

<?php
    if(isset($_POST['sex'])
    {
        $sex = htmlspecialchars($_POST['sex']);
        $sql = 'INSERT INTO mytablename(checkbox_value) VALUES("{$sex}")';

    }
?>

You will need to make a connection to the database first, see here how. I will recommend that you use prepare statements, they are not so funny to work within standard MySQLi connection so when you fill that you know what you are doing so begin to use a PDO connect Connect with PDO

Note: I do not have tested this code before post it. But in any way, it should lead you in the right direction.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda