0

I`ve read all similar questions I found. But most of the answers give solution to not trying to echo the values after submitting.

Here is my code:

<!DOCTYPE html>
<html>

<head>
    <title>Post Request Example</title>
</head>

<body>

<form method="POST">
    <label>Name: <input name="name"/></label><br/>
    <label>Age: <input name="age"/></label><br/>
    <label>Gender:</label><br/>
    <label><input type="radio" name="gender" value="male">Male</label><br/>
    <label><input type="radio" name="gender" value="female">Female</label><br/>
    <input type="submit" name="submit" value="Submit" />
</form>

<?php

if (isset($_POST["name"]) && isset($_POST["age"]) && isset($_POST["gender"])) {
    $name = $_POST["name"];
    $age = $_POST["age"];
    $gender = $_POST["gender"];

    $result = "My name is " . $name . ". Im " . $age . " years old. Im " . $gender . ".";

    echo $result;
}
?>

</body>
</html>

My question is: Why does this code work just fine when using $_GET on submitting and prints the result. And why it returns undefined when I use $_POST for submitting?

This is not the same question as Send value of submit button when form gets posted. There the situation is that the guy uses 2 submit buttons and can't receive their value. My problem is that I submit and the "echo" is not printed because my 3 values are empty. So I can't understand why my 3 variables are empty.

P.S.: I would use jQuery, but I'm on a PHP Course and we have that for homework.

  • Your code works.... – Stuart Sep 02 '17 at 15:12
  • That`s strange. Doesn't work on my side. Maybe it's a config problem? In Network I see the array as JSON, but the echo doesn't work. If I change to GET it works as expected. – Todor Atanasov Sep 02 '17 at 15:16
  • I can confirm that your code works and I don't see any point why it shouldn't. You could check some configuration values in your php.ini as suggested in this post: https://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission – Benni Sep 02 '17 at 21:24
  • Another thing is, are you sure about the JSON? IMO this could only mean you have some javascript that prevents the form submission and then submits the form data as JSON ... in this case you cannot access the data via $_POST["name"], you must use something similar to `$retrieved_form_data = json_decode($_POST["my_JSON"], true); ` and then `$name = $retrieved_form_data["name"];` – Benni Sep 02 '17 at 21:41

0 Answers0