1

I have made a very simple form, just to see if I'm doing the right thing when submitting data in PHP. It just consists of three radio buttons and nothing else. Right not, I don't get the value in $_POST as i expect.

The form:

<form name='testform' action='test.php' method='post'>
    <input type='radio' name='testbutton' value='larry' />
    <input type='radio' name='testbutton' value='curly' />
    <input type='radio' name='testbutton' value='moe' />
    <input type='submit' value='Submit'>
</form>

The script, test.php:

if($_POST['testbutton'] == 'larry') {
    echo "You picked Larry";
} elseif($_POST['testbutton'] == 'curly') {
    echo "You picked Curly";
} else {
    echo "You picked Moe";
}

The code returns no errors but whatever button I choose, I always get Moe, even when selecting no button at all. Using var_dump($_POST) gives nothing, an empty space. Using print_r($_POST) give 1, no matter what I pick. I can't find what I'm doing wrong here.

Btw, I know that this code is not optimal but I'm just testing things here.

Sandokan
  • 861
  • 1
  • 9
  • 18
  • I'm not having a problem with it and I do not see anything wrong :/ Try using $_REQUEST instead to see if it is receiving it at all. – TylerH4 Jun 10 '12 at 23:40
  • Well, it is the whole form, but I have two more atop of it. One with list-elements and one with checkboxes. But they work as the should. Only the radio-buttons act up. Is it possible that those forms could be "called" and some how overrwrite the whole process? I hva checked the tags so the forms are not nested in any way. – Sandokan Jun 11 '12 at 06:02

3 Answers3

3

You probably have some other code not shown to us, which is assigning a value to the $_POST variable, overwriting it.

If you make a brand new php file, and enter nothing more than the code you posted here, it should work fine.

goat
  • 31,486
  • 7
  • 73
  • 96
0

I know this is an old post, but when debugging mysterious problems, sometimes another suggestion might help someone solve their "lack of POST" data mystery.

In my case, I was using ajax to submit a form. In the javascript code: I had set the "type: " to "GET" and then proceeded to write code to handle "$_POST" data in the server-side PHP script. Deceivingly, the view of the Request (as seen in the Chrom Developer tools) has the right values so you are confident that the values are being transmitted. On further examination, however, you see that the Response Headers are showing "GET".

Dumb error for sure. Hope this helps rescue someone else from a similar debugging hell.

Community
  • 1
  • 1
globalSchmidt
  • 1,329
  • 16
  • 28
-1
  • It is always good to add 'name' attribute to input element.
<input type="submit" name="MySubmit" value="Submit">
  • You are always getting Moe when not selecting anything. Assuming that you have not shown the entire code, this is what I can tell you:
if(isset($_POST['MySubmit'])) {

    $selected = $_POST['testbutton'];

    switch($selected){

        'larry': echo "You picked Larry";

                 break;

        'curly': echo "You picked Curly";

                 break;

         default: "You picked Moe";

                  break;

    }

}

else {

    echo "Form submit failed";

}
exception
  • 955
  • 2
  • 11
  • 23
  • Sorry. For some reason, code block format did not work for me for this post. I tried re-editing. If someone else can do that, thank you. – exception Jun 10 '12 at 23:58
  • It is almost never good to name the input button and check for it to detect form submission. http://stackoverflow.com/questions/2680160/how-can-i-tell-which-button-was-clicked-in-a-php-form-submit/2680198#2680198 – goat Jun 11 '12 at 00:15