0

Recently started learning php and am trying to get the get method to work.

This is the html form I am testing on:

<!DOCTYPE html>
<html>

<body>
<form action="foo.php" method="get">
  Name: <input type="text" name="username" /><br />
  Email: <input type="text" name="email" /><br />
  <input type="submit" name="submit" value="Submit me!" />
</form>
</body>

</html>

I researched and found that you should use isset() to check if the $_GET array contains the value. So I tried(this is foo.php):

<?php
  if(isset($_GET['username']))
  {
    echo $_GET['username'];
  }

  else
  {
    echo "username not passed <br />";
  }

  if(isset($_GET['email']))
  {
    echo $_GET['email'];
  }

  else
  {
    echo "email not passed";
  }
?>

It seems something is wrong with trying to pass the values via the get method, I tried the exact same thing with post, replacing method="get" to method="post" and changing $_GET to $_POST in the php file. This worked perfectly with the post method. I know you shouldn't pass sensitive information like email etc via the get method but i am just trying to test it out.

This is the output I get after I submit the form: username not passed email not passed

user3247608
  • 583
  • 3
  • 9
  • 17
  • 1
    Learn the basics of PHP ! – Sliq Feb 22 '14 at 21:14
  • 4
    @Panique "*the basics*"? Doesn't they [manual](http://php.net/manual/en/function.isset.php) say that `isset()` can be used to check if an index is set in an array? Perhaps you could answer this question and teach those of us who use it in this fashion the proper way to do it instead of adding snarky and unhelpful remarks? – newfurniturey Feb 22 '14 at 21:18
  • 4
    at face value, there's nothing wrong with your code. Are you sure the current incarnation of your 2 pieces of code are lined up (method is get and using $_GET or w/ same with post; IOW not method get but using $_POST or visa versa)? Are both these bits of code in the same file foo.php and you're just seeing that msg on initial load? – CrayonViolent Feb 22 '14 at 21:18
  • 1
    @newfurniturey yes the manual does, and this code does not produce a undefined index. The OP is not running it as the above example, cause then he would have no errors. So I could agree to learn the basics, just try and copy paste the code and try yourself. It does nomatter if its post or get – Ronni Skansing Feb 22 '14 at 22:13
  • The html and php are in different files, html in html_form.html and php in foo.php. I load the html file enter in a name and email address and click submit. I then get username not passed, email not passed. – user3247608 Feb 23 '14 at 04:56
  • Ok, just got it working. I don't know why it wasn't working before. I restarted the server with sudo service httpd restart and it worked. – user3247608 Feb 23 '14 at 04:59

1 Answers1

0

What happens when you try this method?

if(array_key_exists('username', $_GET))
    echo $_GET['username'];
Magictallguy
  • 622
  • 4
  • 16