keewee279
keewee279

Reputation: 1654

PHP: Submitting form data via $_POST works for all fields except radio buttons

I am new to PHP and hope someone can help me with this.

I have an HTML form with a number of inputs and textareas. On Submit I pass the form to another PHP page that generates an email with its values.

For text inputs and textareas everything works as intended but for the radio buttons I can't get it to show me the value on the targetted page.

The radio buttons look as follows and the classes used there are only to apply some CSS. There is only one form on the submitting page, all radios have the same name ("requestType") and there are no other elements using this name.
Also, I added a quick JavaScript snippet for testing to alert their value on change and this worked as well, so the issue seems to be only with the $_POST.

Can someone tell me what I am doing wrong here or what alternatives I could try here ?

My HTML (on the submitting page):

<input type="radio" class="customRadio radioDefault" id="requestType1" name="requestType" value="Value1" />
    <label for="requestType1">Value1</label>
<input type="radio" class="customRadio triggerDiv" id="requestType2" name="requestType" value="Value2" />
    <label for="requestType2">Value2</label>

My PHP (on the targetted page):

$_POST["requestType"]

Update:
As per RiggsFolly I tried to check whether it recognises that a radio button is checked which it seems it does, it just doesn't return anything as the following just returns "xx":

if(isset($_POST['requestType'])){
    $theSelectedOne = $_POST['requestType'];
    echo "radio value: x" . $theSelectedOne . "x";
}else{
    echo "boohoo";
}

Upvotes: 0

Views: 211

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

Radio buttons (and Checkboxes) are only passed back to the form in either the $_POST or $_GET arrays if they are actually checked. I notice you do not auto check one of them as you create the HTML so nothing is likely to be returned if the user makes no selection.

So the best way to test if they were checked or not is to test for the existance of their name in the $_POST array

if ( isset( $_POST['requestType'] ) ) {
    // a selection was made
    // so now test the value to see which one was checked
    $theSelectedOne = $_POST['requestType'];
}

Upvotes: 1

Related Questions