3

I have two pages, in the first page I submit the form which contains Radio buttons.

From the second page, I try to read the responses from the Radio buttons using the $_POST.

The problem is when I hit submit, the $_POST in the second page reads NULL. But when I'm not disabling the Radio buttons in the first page the $_POST form the second page reads the data from the Radio butons correctly.

Is there any solution to make the $_POST reads the values of disabled Radio buttons?

I'm using this Javascript code to disable the radio buttons when the user clicks on it:

function disableRadio(groupName){
        var radio=document.getElementsByName(groupName);
        for(c=0;c<radio.length;c++)
            radio[c].disabled=true;
}

Here is a simple code from each of the two pages.

Page1:

echo '<form action="Page2.php" method="post">',
     "<input type='radio' name='Question1' value='1' onClick='disableRadio(Question1)' />",
     "<input type='radio' name='Question1' value='2' onClick='disableRadio(Question1)' />",
     "<input type='radio' name='Question1' value='3' onClick='disableRadio(Question1)' />",
     "<div><input type='submit' value='Submit'></div>",
     "</form>";

Page2:

$response=$_POST['Question1'];
echo $response;

Update to this question

When using the ReadOnly attribute, the Radio buttons in the group are still click-able. So, I decided to Disable the other Radio buttons. Is there a better solution, as I don't want the user to have the feeling that he is able to change the answer.

user1483799
  • 409
  • 4
  • 8
  • 17

5 Answers5

4

Disabled inputs do not submit their values.

You could set them to be readonly instead, if this behaviour suits your situation.

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
2

Q: Is there any way to get sound out of my handset after I've hit the "off" button?

A: No, try hitting "mute" instead :)

There's a difference between "disabled" (this input is turned OFF) and "readonly" (has a value, but user can't tamper with it):

'Hope that helps .. PSM

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • How to do it in javascript? I tried like this but it didn't work: radio[c].readonly="readonly"; – user1483799 Jul 25 '12 at 21:35
  • 1
    When using the ReadOnly attribute, the Radio buttons in the group are still click-able. So, I decided to Disable the other Radio buttons. Do you have a better solution, as I don't want the user to have the feeling that he is able to change the answer. – user1483799 Jul 26 '12 at 23:49
1

You can set a hidden value to use a proxy for the radio button, and depending on the radio button's state set it, that way you can still disable the radio button and always have a value for what you want.

Darren
  • 10,631
  • 8
  • 42
  • 64
0

You should use readonly instead of disabledon the input elements.

Florian
  • 3,366
  • 1
  • 29
  • 35
  • How to do it in javascript? I tried like this but it didn't work: radio[c].readonly="readonly"; – user1483799 Jul 25 '12 at 21:37
  • `radio.readOnly = true` should do it. (It's the capital O, I'd also strongly advise to use jQuery, if you have the option) – Florian Jul 25 '12 at 21:43
0

You may be better off using <input type="hidden">, and if you really need the user to see what they submitted earlier then you can add a set of disabled radio buttons with no name to show it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I think I prefer the readonly option to be honest which quite a few others have mentioned. Is there a disadvantage that you see to using readonly? – Jon Taylor Jul 25 '12 at 21:20
  • 1
    @JonTaylor - A little outdated, but it might help answer your question: http://kreotekdev.wordpress.com/2007/11/08/disabled-vs-readonly-form-fields/ OR here http://stackoverflow.com/questions/7730695/whats-the-difference-between-disabled-disabled-and-readonly-readonly-for-te – Paul Dessert Jul 25 '12 at 21:28