Getting radio button value by ajax

Getting radio button value by ajax

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Radio Buttons Return Zero in AJAX Calls

When you submit a form populated with radio buttons via AJAX, receiving 0 or empty strings instead of the selected option's value is a classic jQuery pitfall. The code snippet provided demonstrates exactly this scenario: a form submission handler captures input values and sends them to edit_check.php. Despite the logic appearing sound at first glance, the underlying mechanism of how radio buttons function in HTML contradicts the standard text input handling used in the script.

The root cause lies in how radio buttons interact with their parent forms versus individual text inputs. Unlike <input type="text"> or <input type="hidden">, which store their value directly within the element, radio buttons are name-based. When you execute $("#message_pri").val(), jQuery looks for an element with the ID message_pri. However, in the provided HTML, the ID is only assigned to the first input in that group. The subsequent inputs share the same name attribute (name="message_pri") but lack unique IDs.

Because radio buttons belong to a mutually exclusive group defined by their name attribute, the browser's DOM model treats them differently than independent elements. When you call .val() on an element that is not currently checked, jQuery returns an empty string or null. In many contexts, this renders as 0 when serialized into JSON or sent via POST depending on how the data is processed on the backend. The script attempts to grab the value of $("#message_pri"), which technically exists (the first radio button), but if that specific checkbox isn't checked, .val() returns nothing. You aren't getting the value of the selected button; you are getting the value of the element identified by the ID, regardless of its state.

To resolve this, you must explicitly select the checked element within the group before retrieving its value. The correct approach is to use a selector that filters for the :checked state. Instead of querying by ID alone, you should query by the name attribute combined with the checked state.

Here is the corrected logic for capturing these values:

$("#save_privacy").submit(function() {
    // Select the checked radio button within the group named 'message_pri'
    var message_pri = $("input[name='message_pri']:checked").val();
    
    // Repeat for other groups
    var follow_pri = $("input[name='follow_pri']:checked").val();
    var post_pri = $("input[name='post_pri']:checked").val();

    // The hidden input works as expected because it is a single element
    var check7 = $("#check7").val();

    var s = {
        "message_pri": message_pri,
        "follow_pri": follow_pri,
        "post_pri": post_pri,
        "check": check7
    };

    $.ajax({
        url: 'edit_check.php',
        type: 'POST',
        data: s,
        beforeSend: function() {
            $(".privacy_info").html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
        },
        success: function(data) {
            $(".privacy_info").html(data);
        }
    });

    return false;
});

This adjustment ensures that jQuery ignores the unchecked inputs and strictly targets the one currently selected by the user. If no radio button in a group is checked, .val() will still return empty, which you should handle gracefully on the client side to prevent sending undefined values to your server.

On the backend, specifically in edit_check.php, ensure you are handling the incoming POST data correctly. In PHP, accessing a name-based array from multiple unchecked radio buttons often results in an empty string rather than 0 unless the form is processed using specific array wrapping techniques (like ['name' => []]). However, since the script now passes the correct selected value via the jQuery selector change, the server should receive the intended 1, 2, or 3.

If you are maintaining a legacy codebase like this, it is worth noting that while modern frameworks handle form data serialization differently, understanding these DOM quirks remains essential. For developers moving toward Laravel, leveraging the $request->input('name') method handles radio groups automatically by returning the value of the checked element, abstracting away the jQuery selector logic entirely.

Our network