-1

I have this issue that my form obviously doesn't send data with POST method but it sends it with GET method. Here is my HTML code of the form

<form action="action.php" method="POST">
        <input type="text" name="text">
        <input type="submit" value="send">
</form>

and here is the php code in the action page

if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo $_POST['text'];
var_dump($_POST);
}

if(isset($_POST['text'])){
echo "ok";
}else{
echo "no";
}

when I submit the form I get this error for output

Notice: Undefined index: text in F:\test\action.php on line 9
array(0) { } no

but when I send data with GET method it works correctly without any problem. I think the problem is for phpstorm because it runs correctly in the xampp server. and the considerable thing is when I run it in mozila or IE it says page not found but xampp is okay.

Mohsen
  • 3
  • 5
  • post isnt the same as get. "get is like opening a new page with the URL Attributes u defined" . "post is to open a new page and send data to it." try to define the Action in ur form like php_self and post ur error then – KikiTheOne Sep 07 '16 at 13:26
  • Are you sure that the error occurs when submitting the form? You should move all the code accessing the `$_POST` array inside your `if($_SERVER['REQUEST_METHOD'] == 'post')`. – mario.van.zadel Sep 07 '16 at 13:26
  • 1
    `echo $_POST['text'];` will give you error on page load it should be inside your if() condition – Indrajit Sep 07 '16 at 13:27
  • as @Indrajit said. remove the echo $_POST['text']; after ur IF then ur arrow should be gone. or just add an "@" infront -> `echo @$_POST['text'];` – KikiTheOne Sep 07 '16 at 13:30
  • So .. what URL so you see in a browser when POST does not work? Based on your last sentence it could indeed be PhpStorm's issue (in case if you use PhpStorm's built-in simple web server) – LazyOne Sep 08 '16 at 09:49
  • @LazyOne when I click send button this show in the URL `http://localhost:63342/test/action.php` and the error appears.and I'm using chrome that shows these errors but in mozila an internet explorer it says page not found!!!!!!!! – Mohsen Sep 08 '16 at 09:56
  • @Mohsen You are using PhpStorm's built-in simple web server that at the moment has some random wierd issues with serving POST requests. I suggest to switch to a proper web server (e.g. Apache from your XAMPP) to serve your web pages. http://stackoverflow.com/a/34787827/783119 . Built-in web server is used if you do not have any deployment server specified in the project. – LazyOne Sep 08 '16 at 10:14
  • @LazyOne I'm not familiar a lot with phpstorm, if you could tell me where should I switch it that would be great. and my projects are not in the htdocs in xampp folder they are in another drive if this helps. – Mohsen Sep 08 '16 at 10:48
  • @Mohsen https://confluence.jetbrains.com/display/PhpStorm/Deployments+in+PhpStorm – LazyOne Sep 08 '16 at 10:51

2 Answers2

0

Try using isset with your input like so:

You have to add name="something" for the isset to pick up that you have clicked it.

<?php

    if (isset($_POST['sub']))
    {
        echo $_POST['text'];
    }

?>

<form action="" method="post">
    <input type="text" name="text">
    <input type="submit" name="sub" value="Submit">
</form
Option
  • 2,605
  • 2
  • 19
  • 29
0

I can only assume that the output you are seeing is before you submit the form. When you submit it, you should check for POST and not for post:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                                    ^^^^ here
    echo $_POST['text'];
}
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • It's because they have their full code in the same file and is triggered by this `echo $_POST['text'];` not being inside the conditional statement. – Funk Forty Niner Sep 07 '16 at 13:40
  • @Fred-ii- That's why I am assuming that the output is from before the form submit :-) – jeroen Sep 07 '16 at 13:44
  • The notice is happening on line 9 being `echo $_POST['text'];`. Since their form contains 4 lines, it's deduced that line 9 is their `echo $_POST['text'];` ;-) – Funk Forty Niner Sep 07 '16 at 13:45
  • @Fred-ii- That's some clever deducing Sherlock ;-) – jeroen Sep 07 '16 at 13:46
  • @Fred-ii- I actually thought that the section below the `if` was just some debugging stuff as the `if` statement will always fail... – jeroen Sep 07 '16 at 13:48
  • @Fred-ii- But we probably will never know :-( – jeroen Sep 07 '16 at 13:50
  • Yes we will (know) because I just tested their code on my machine and gotten the same error. I then moved the `echo $_POST['text'];` that's above the var_dump inside the conditional statement and the notice disappeared ;-) `== 'post'` worked btw. – Funk Forty Niner Sep 07 '16 at 13:53
  • @Fred-ii- @jeroen when I changed `post` to `POST` it gets into the `if` statement but again I get `undefined index: text` error – Mohsen Sep 07 '16 at 20:42