Crash893
Crash893

Reputation: 11702

radio button value in php

I'm trying to make a simple survey in php

I have a set of radio buttons on a page called sja.php that sends its to sjamail.php page

the problem is that when I go to get

$answer = $_POST['ans'];

I can't seen to do anything like

echo "$answer";

but if I were to throw some logic at it

like

    if ($answer == "ans1") {

        echo 'Correct';
    }

   else {

       echo 'Incorrect';
    }    

It will display correct or incorrect (edit: The if/else works correctly and will display the correct answer )

so why is it I can't access the value of the radio button "ans" as a string?

http://www.markonsolutions.com/sja.php

print_r($_POST); will return Array ( [ans] => )

Upvotes: 0

Views: 14826

Answers (5)

Lea
Lea

Reputation: 1

Try this:

$answer = (string)$_POST["ans"];
echo $answer;

You must convert $_POST["ans"] to string.

Upvotes: 0

Guasqueño
Guasqueño

Reputation: 447

I had a similar problem with the following:

     <input name="03 - Gender" type="radio" value="Masculino"/>Male<br/>
     <input name="03 - Gender" type="radio" value="Femenino" required="required"/>Female <br/>
     <input type="hidden" name="03 - Gender" value=""/>

but when I removed the third input line (the hidden one) the problem desapeared.

Upvotes: 0

Marc B
Marc B

Reputation: 360562

Your page works correctly if you select any of the first 4 radio buttons (ans1/2/3/4). But the rest of the radio buttons next to all those images have blank values, which would explain why your posted value is empty if you selected any of those to test with.

Upvotes: 1

Brant Messenger
Brant Messenger

Reputation: 1451

You need to make sure that the field in HTML has...

<input type="radio" name="ans" value="ans1" />
<input type="radio" name="ans" value="ans2" />

Also make sure your form method is POST

Upvotes: 1

david
david

Reputation: 33507

Perhaps the value is something other than text.

Try

var_dump($answer);

or

print_r($answer, TRUE);

Upvotes: 2

Related Questions