0

I am coding a PHP script, but I can't really seem to get it to work. I am testing out the basics, but I don't really understand what GET and POST means, what's the difference? All the definitions I've seen on the web make not much sense to me, what I've coded so far (but since I don't understand POST and GET, I don't know how to get it to work:

    <form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email:&nbsp; <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['mail_sub']))
    {

    echo $_POST['theirname'];

    }
?>
Reverb
  • 835
  • 2
  • 11
  • 16

7 Answers7

2

$_POST isn't working for you because you have the form method set to get.

<form name="mail_sub" method="post">

There's plenty of better info online as to the difference between post and get so I won't go into that but this will fix the issue for you.

Change your PHP as well.

if ( isset( $_POST['theirname'] ) ) {

    echo $_POST['theirname'];

}
Nathan Dawson
  • 14,920
  • 3
  • 36
  • 45
2

To answer the question:

GET and POST are one of the many request types in the standards of internet.

The difference is that GET can't post data, parameters will be appended to the url (url-parameters) which has it's limitations. POST does post parameters/data.

The standard is:

  • GET for getting data.
  • POST for creating data.
  • PUT for updating data.
  • DELETE for removing data.
Tim Baas
  • 5,585
  • 5
  • 42
  • 66
1

Post is like "sending" the data to the page without giving the user having any interaction with it, when Get shows the parameters in the URL and making it public for the user. Get is more often used on "unsecure" type of datatransactions like for example a searchform and POST is used when you want to make more secure things like a login form.

Using a Get will give you like index.php?parameter=hello&parameter2=world

In your example you should use either POST in the method attribute in the formtag or $_GET in the php section

So either

<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email:&nbsp; <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['theirname']))
{

echo $_POST['theirname'];

}
?>

or

<form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email:&nbsp; <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_GET['theirname']))
{

echo $_GET['theirname'];

}
?>
Thomas
  • 87
  • 6
  • from security point - there is no difference, actually (post is easy accessible as get). GET request is limited to ~250 chars, if i remember well... – sinisake Feb 14 '14 at 07:21
  • 1
    POST is not any more secure than GET. You may not be able to "see" the POST parameters in the URL, but you could easily open up your browser inspector and see the headers that were sent with a POST request, allowing you to see the parameters. That also means that any malicious user could use their browser tools or `curl` to manipulate the parameters and craft their own POST request. – Alvin S. Lee Feb 14 '14 at 07:25
  • yeah, you are right! I wrote that cause that's what's IMO a typical use of them two without having to write a long guide :) – Thomas Feb 14 '14 at 07:27
  • 1
    Just another difference: You can POST binary data, you can't send it via GET. – webnoob Feb 14 '14 at 07:28
1

replace

isset($_POST['mail_sub'])

by

isset($_POST['theirname'])

The main difference between GET and POST requests is that in GET requests all parameter are part of the url and the user sees the parameters. In POST requests the url is not modified and all form parameter are hidden from the user. If you have no file uploads or very long field parameter use GET. Use POST when going in production.

WeSee
  • 2,032
  • 1
  • 18
  • 42
0

Replace Your Code:

<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email:&nbsp; <input type="text" name="theirpass"> <br />
<input type="submit" name="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['submit']))
    {

    echo $_POST['theirname'];

    }
?>
Guru
  • 623
  • 1
  • 4
  • 12
0

you used method as GET in form,so your code should be like this

<?php echo $_SERVER['PHP_SELF'];?>//the form and action will be same page 

  <form name="mail_sub" method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="theirname"> <br />
  Email:&nbsp; <input type="text" name="theirpass"> <br />
  <input type="submit" value="Join!" style="width:200px">
  </form>
  <?php
   echo $_GET['theirname'];
  ?>
user3064914
  • 893
  • 1
  • 7
  • 18
0

GET - If form method is GET then on submitting the form, Form will be submitted to

xyz.com/submit.php?theirname=john&theirpass=password   // CHECK HERE - form elements are in URL so not so secure

and can be checked by PHP as

if(isset($_GET['their']))
{
$name=$_GET['their'];  //name will return John
}

POST - If form method is POST then on submitting the form, Form will be submitted to

xyz.com/submit.php   // CHECK HERE form elements are not in URL

and can be checked by PHP as

if(isset($_POST['their']))
{
$name=$_POST['their'];  //name will return John
} 
Akash Kumar
  • 592
  • 6
  • 18