0

Not sure what the heck I am doing wrong here as I am trying to learn how to process forms and followed this tutorial(http://www.w3schools.com/php/php_forms.asp) to a T and still couldn't get it to work. You'll see some minor differences, but I have been trying to get this working for the last hour and have added different things to try and fix it.

Here is my first file:

    <html>
<body>

<form action="data.php" method="post" enctype="multipart/form-data">
    Name: <input type="text" name="test"><br>
    <input type="submit">
</form>

</body>
</html>

Here is my data.php

<?php

if(isset($_POST['test'])){
    $name = $_POST['test']; }

?>


Welcome <?php echo $name; ?><br>

I added a var_dump($Globals)

  'HTTP_RAW_POST_DATA' => string 'test=asdf' (length=9)
  '_GET' => 
    array (size=0)
      empty
  '_POST' => 
    array (size=0)
      empty
  '_COOKIE' => 
    array (size=1)
      'Phpstorm-67f10f2' => string '2e6a5757-50fb-4c36-8bd7-0fe98e66892d' (length=36)
  '_FILES' => 
    array (size=0)
      empty
  '_ENV' => 
    array (size=0)
      empty
  '_REQUEST' => 
    array (size=0)
      empty
  '_SERVER' => 
    array (size=78)

I am baffled.

Thanks in advance.

Matt
  • 1
  • 3
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Hamza Zafeer Jun 14 '16 at 03:44
  • are you actually inputting a name? it's better to use `!empty()` instead of `isset()` btw – ksealey Jun 14 '16 at 03:44
  • Fair question. Yes, I am inputting text into the form. – Matt Jun 14 '16 at 03:46
  • i test your codes and its working fine. – Ashraf Kamarudin Jun 14 '16 at 04:47
  • unfortunately not for me. However, I have gotten it to work by using GET/$_GET as well as GET/$_REQUEST. It is only the POST method that is not working. – Matt Jun 14 '16 at 12:51

4 Answers4

0

The variable's only set to the $_POST value if it exists but you're asking PHP to display it under all circumstances regardless.

Place the echo command in the if statement along with the other bit of code.

JackHasaKeyboard
  • 1,599
  • 1
  • 16
  • 29
  • 1
    I added an echo into the if statement and moved the "welcome.." into it as well. I now get no error but nothing is displayed – Matt Jun 14 '16 at 03:38
0

Please try it

<?php
if(isset($_POST['test'])){
    echo 'Welcome ' . $_POST['test'] . '<br>';
}
?>
Atom
  • 23
  • 1
  • 3
  • Thanks for the syntax help. Still not echoing anything. If I drop a var_dump($_post) i get a null value – Matt Jun 14 '16 at 03:56
0

your form is correct.. just write print_r($_POST) to see what all data is carried in the post request... and for getting value of test write

$name=$_POST["test"];
echo $name;
Tirthraj Barot
  • 2,671
  • 2
  • 17
  • 33
  • For some reason it is not working on my end! However, I changed everything to GET and it works just fine. I am 100% confused since it looks like everything is correct, but it just doesn't like the POST method. – Matt Jun 14 '16 at 12:49
  • The POST request encapsulates your data from client side and sends it to server side while the GET request appends your data to url and sends it to server side... Both have different purposes.. POST is used for larger data while GET is used for smaller data. @Matt – Tirthraj Barot Jun 14 '16 at 12:51
  • I am somewhat familiar with the differences and know that I do not want to use the _GET unless I absolutely have to. However, based on my situation would you say something is wrong with my WAMP server configuration? – Matt Jun 14 '16 at 12:53
  • try to get errors by pasting these commands in your php on first line. error_reporting(E_ALL); ini_set("display_errors", 1); – Tirthraj Barot Jun 14 '16 at 12:54
  • I have edited the main question to show the file at this point. – Matt Jun 14 '16 at 13:10
  • This must mean it is not a syntax error, but something wrong with my server configuration on WAMP. However, it was setup as default so I am a bit frustrated that it wouldn't work with this right out of the gate. I guess I'l continue to search. – Matt Jun 14 '16 at 13:38
  • and I would be glad if i get the answer correct &or vote up from you. @Matt – Tirthraj Barot Jun 14 '16 at 15:59
0

I was able to solve this problem myself by looking through a variety of different posts relating to "$_GET but $_POST doesn't.

There were two things I changed.

First, I was using wamp and my /root folder was not in the /www but rather wamp/root/index.php.

I also added an interpreter in phpstorm by going to FILE|Settings|Build, Exe...|Deployment and added a local interpreter direction to C:\wamp\bin\php\php5.5.12.

Not sure which one solved the issue, but it now works.

Matt
  • 1
  • 3