how do I disable all other radio buttons while one is selected in php

Dani AI

Generated

and gave useful pointers, but the key clarification is this: PHP runs on the server and cannot change controls in the browser after the HTML has been sent — real-time enabling/disabling must be done in the client (browser). Browsers already enforce single selection inside a same-named radio group, so if you need to disable radios outside that built-in grouping you must use client-side code. See the PHP introduction and the radio-input reference for details. PHP manual — IntroductionMDN: input type="radio". (php.net)

A simple, robust client-side pattern (vanilla JavaScript) is: listen for change on radios, and when one becomes checked disable all other radios. Keep the checked radio enabled so its value is submitted with the form (disabled controls are excluded from submission). Add a “clear” or “change answer” control so users can re-enable the options if needed. Example JavaScript:

// disable other radios when one is checked
document.addEventListener('change', function (e) {
  if (!e.target.matches('input[type="radio"]') || !e.target.checked) return;
  document.querySelectorAll('input[type="radio"]').forEach(function (r) {
    if (r !== e.target) r.disabled = true;
  });
});

// optional: a button with id="clearRadios" to reset and re-enable
document.getElementById('clearRadios').addEventListener('click', function () {
  document.querySelectorAll('input[type="radio"]').forEach(function (r) {
    r.disabled = false;
    r.checked = false;
  });
});

Remember that disabled makes a control unfocusable and omitted from form submission; you can toggle the attribute from script (or use Element.toggleAttribute) when appropriate. MDN: disabled attributeMDN: Element.toggleAttribute. (developer.mozilla.org)

Accessibility and UX notes: disabling all other choices prevents easy changes of mind and may confuse keyboard/screen-reader users. Consider providing a clear “Change answer” action, or use an ARIA approach for custom controls (see aria-disabled) if you must preserve focus/submission behavior. Test the flow across browsers and assistive tech before deploying. MDN: ariaDisabled / aria-disabled. (developer.mozilla.org)

In short: implement the disabling in client-side script (as shown) and use PHP only to render initial states or to persist the choice on the server between requests.

Recommended Answers

All 2 Replies

If you put the radio buttons into a group, only one is allowed to be selected at any given time.

<input type="radio" name="group1" value="button1">
<input type="radio" name="group1" value="button2">
<input type="radio" name="group1" value="button3">
<input type="radio" name="group2" value="button4">
<input type="radio" name="group2" value="button5">
<input type="radio" name="group2" value="button6">

Hi,
As per "pixesoul" you can radio buttons as group
Or
You can use jQuery to disable all other radio buttons.

If you need more help, Please let me know.

Thanks,
Ajay

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.