Sandokan
Sandokan

Reputation: 849

Form value not found in _POST

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.

Upvotes: 1

Views: 3090

Answers (3)

globalSchmidt
globalSchmidt

Reputation: 1339

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.

Upvotes: 0

exception
exception

Reputation: 945

  • 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";

}

Upvotes: -1

goat
goat

Reputation: 31834

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.

Upvotes: 3

Related Questions