Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!