MuriloRM
MuriloRM

Reputation: 79

I can send form data using GET method but not POST, why?

So I'm trying to learn html+php but it seems like I've hit a wall. If I use the GET method in my html form, parameters are sent to my php file just fine, but if I try to do the same using the POST method no parameters are sent.

@Edit: I've taken down the initial code sample displayed here as I found out its not a problem specific to that code. Instead I'm posting a basic form and a basic php script that have the same problem:

HTML FILE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="testingForm.php" method="POST">
        INPUT: <input type="text" id="iTesting" name="nTesting"/><br/>
        <input type="submit" value="SUBMIT"/>
    </form>
</body>
</html>

PHP FILE:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title></title>
</head>
<body>
<?php
    /* THIS WORKS: */
    /*if (isset($_GET["nTesting"]))
        echo "It is working! ".$_GET["nTesting"];
    else
        echo "It is NOT working! input: ".$_GET["nTesting"];
    echo "<br/>".$_SERVER['REQUEST_METHOD']."<br/>";
    echo "<br/>".var_dump($_GET);//*/

    /* THIS DOESN'T: */
    if (isset($_POST["nTesting"]))
        echo "It is working! ".$_POST["nTesting"];
    else
        echo "It is NOT working! input: ".$_POST["nTesting"];
    echo "<br/>".$_SERVER['REQUEST_METHOD']."<br/>";
    echo "<br/>".var_dump($_POST);//*/
?>
</body>
</html>

As stated before, if I change the form method to GET, I get no problem at all. However, data doesn't seem to be sent when using POST method.

This is the output using the GET method:

It is working! input: test

GET

array(1) { ["nTesting"]=> string(4) "test" }

This is the output using the POST method:

It is NOT working! input:

POST

array(0) { }

Also, using the developers tool I can see there is a parameter nTesting:test in the formData section of the network tab. Yet, nothing is displayed.

Upvotes: 2

Views: 3435

Answers (1)

wsantos
wsantos

Reputation: 74

If you're using Chrome

  1. Go to index.html
  2. Press f12. Developer tools will show up
  3. Click Network
  4. Tick Preserve Log and Disable Cache (I usually just do it that way)
  5. From index.html click the submit button (do not close dev tools)
  6. Go back to developer tools click 'teste.php'
  7. Click headers, then expand general. You will see 'Request Method'. It should be POST

After working with @MuriloRM we ended up with the fundamental bug with PHPStorm

Please vote up the resolution of the issue https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000097930-Can-not-use-POST-method-in-PhpStorm

Upvotes: 2

Related Questions