Hello.

When a radio button value is not required and is not selected, the $_POST var ignores its existence unlike text boxes that are left empty.

How to get at least a '' (empty but exists) value so that the radio button var goes along for the ride when the $_POST array is constructed BUT with one catch:

I would like to NOT use PHP on the form to accomplish this. I'd like the form to be just the normal HTML <input type='radio' name='rad' value=?????> whereby maybe there can be a hidden default value if the radio button is not selected(?).

Thanks.

    I usually put a hidden input in HTML before the radio with the same name. If the radio is ticked, it will overwrite the value in POST with the radio value. If it is not ticked, it'll take the value of the hidden input.

      Thanks Desdinova.

      That's kind of what I thought. It's good to know someone actually tried it.

      I just tested it and it works beautifully.

      For checkboxes, do you use a hidden input when you don't use a default value?

      Thanks.

        Thanks again for the info.

        I will mark the thread 'Resolved.'

          Some comments about this "solution" (as well as "problem"):

          sherry wrote:

          When a radio button value is not required and is not selected, the $_POST var ignores its existence unlike text boxes that are left empty.

          Not true. PHP doesn't "ignore" anything it's given. The problem is that unselected radio buttons and/or checkboxes aren't submitted at all by the client's browser (as per W3C standards). PHP can't ignore something that it was never made aware of!

          sherry wrote:

          How to get at least a '' (empty but exists) value so that the radio button var goes along for the ride when the $_POST array is constructed

          I have to ask... why? Why would you even want an "empty but exists" value at all? If the user didn't select an answer for a given radio button group, then it doesn't make sense for it to exist.

          sherry wrote:

          I usually put a hidden input in HTML before the radio with the same name. If the radio is ticked, it will overwrite the value in POST with the radio value. If it is not ticked, it'll take the value of the hidden input.

          Seems like a kludge, to me. Have you tested this in several browsers (including different versions and O/S's) to verify that the behavior is consistent?

            Thanks for the info, bradgrafelman and great questions.

            Point well taken with respect to PHP and submitted form data.

            Regarding 'empty but exists', it is needed because the data is being stored. When the radio button is not selected and therefore the var/value doesn't exist, the record has less vars than other data records. In order to make each record structure the same regardless of what fields are filled in or left empty, that var has to exist, even if it's empty.

            I'm glad you brought up the difference between browsers as I hadn't considered that as it pertained to the hidden input fields used in this app.

            I checked IE8, FF3.6.x and Chrome 9 in Windows 7 and each accepted that hidden value. Hopefully, other OS' and browsers follow suit.

            Thanks much!

              sherry;10973918 wrote:

              Regarding 'empty but exists', it is needed because the data is being stored. When the radio button is not selected and therefore the var/value doesn't exist, the record has less vars than other data records. In order to make each record structure the same regardless of what fields are filled in or left empty, that var has to exist, even if it's empty.

              Next to this, the hidden value does not have to be empty. It is a pretty good way of setting a fallback value as well.

              One of the advantages I see is that you can keep logic like this within the HTML, without having to hard code a value from PHP (which may not be the best place to hard code it in terms of reusability).

              Besides, it's faster, and gives a better overview if you watch the HTML of the form. You're gonna know exactly what value you're gonna get without having to check PHP for it.

                Thanks for the additional feedback, Desdinova.

                  Write a Reply...