http://www.w3schools.com/php/php_forms.asp

On the page above I read that GET variables are send via the url and POST variables are sent as an HTTP request. When I tried POST with a simple php script all of my POST variables were still in the URL which is what I was expecting for GET. Could someone clear this up for me? Thanks.

Recommended Answers

All 5 Replies

Can you show your form code?

Anyway you can setup a simple echo script (echo.php) and see what really happens when a request is sent. You need to open two terminal windows.

Start with this script:

<?php

if($_POST)
    echo 'POST:' . PHP_EOL . print_r($_POST, TRUE);

if($_GET)
    echo 'GET:' . PHP_EOL . print_r($_GET, TRUE);

if($_FILES)
    echo 'FILES:' . PHP_EOL . print_r($_FILES, TRUE);

if($_COOKIE)
    echo 'COOKIE:' . PHP_EOL . print_r($_COOKIE, TRUE);

Then start the internal server in the first terminal:

php -S localhost:8000

And send your requests to this address: http://localhost:8000/echo.php.
From the second terminal window use curl or httpie and send some requests, for example:

http -vf :8000/echo.php id==123 cat=fruit color=red

Here we will append a variable to GET id=123 and two variables to POST cat=fruit&color=red, the request will look like this:

POST /echo.php?id=123 HTTP/1.1
Accept-Encoding: gzip, deflate, compress
Content-Length: 19
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: localhost:8000

cat=fruit&color=red

This is what you would type into a telnet session. As you can see the GET is appended in the first statement:

POST /echo.php?id=123 HTTP/1.1

Following there are the request headers, while post variables are appended to the request body, which comes one blank line after the request headers.

If you upload a file, for example:

echo "Hello World" > /tmp/test.txt

and send the request:

http -vf :8000/echo.php id==123 cat=fruit color=red file@/tmp/test.txt

You get:

POST /echo.php?id=123 HTTP/1.1
Accept-Encoding: gzip, deflate, compress
Content-Length: 334
Content-Type: multipart/form-data; boundary=8b41bb14137f4b60a4560f05e149051c
Host: localhost:8000
User-Agent: HTTPie/0.8.0

--8b41bb14137f4b60a4560f05e149051c
Content-Disposition: form-data; name="cat"

fruit
--8b41bb14137f4b60a4560f05e149051c
Content-Disposition: form-data; name="color"

red
--8b41bb14137f4b60a4560f05e149051c
Content-Disposition: form-data; name="file"; filename="test.txt"

Hello World

--8b41bb14137f4b60a4560f05e149051c--

Bye!

Thanks, I'll give that a try.

Here's my code.

<?php

require_once('init.php');

$inmates = Inmate::get_inmates('COURT');

foreach($inmates as $inmate)
{
    print $inmate->get_property('first_name') . " "  . $inmate->get_property('last_name') . " " . $inmate->get_property('number')  . "<br />";
}

?>

<html>
    <body>
    <br />
    <br />
        <form method='post'>
            Username: <input type='text' name='username'><br /><br />
            Password: <input type='text' name='password'>
            <input type='submit' value='submit'>
        </form>
    </body>
</html>

<?php

echo $_POST['username'] . '<br />';
echo $_POST['password'] . '<br />';

?>

The form works fine for me, it executes a POST request and the parameters are sent as request body, not in the GET segment. I cannot execute the code of the Inmate class, how is this related with the issue?

The only problem with current setup, a part the Inmate code block, is that those print statements at lines 28 and 29 will raise a notice for undefined index, when the page is accessed through a GET request or if those indexes are not set in the POST request.

Also the HTML must have an <head> part.

However, both issues should not cause the problem you are reporting.

I wasn't getting an error or anything it's jut that I noticed that my POST variables appeared in the URL, .com?var=x&var=y
What do you mean that my HTML MUST have a <head> tag? I'v never had an issue from leaving it off. I don't always need one, espesially when it's just a small page meant mainly for a little PHP output.

commented: correct +14

What do you mean that my HTML MUST have a <head> tag? ...

It seems I'm still learning the basics. You're right, it's not required, what is required is the title tag, an not even in all cases:

If the document is an iframe srcdoc document or if title information is available from a higher-level protocol: Zero or more elements of metadata content, of which no more than one is a title element and no more than one is a base element.
Otherwise: One or more elements of metadata content, of which exactly one is a title element and no more than one is a base element.

And:

The title element is a required child in most situations, but when a higher-level protocol provides title information, e.g. in the Subject line of an e-mail when HTML is used as an e-mail authoring format, the title element can be omitted.

In practice:

<!DOCTYPE html>
<html>
    <title>Hi</title>
    <body>
        <p>...</p>
    </body>
</html>

is valid. Sorry and thanks for pointing that out.

I wasn't getting an error or anything it's jut that I noticed that my POST variables appeared in the URL, .com?var=x&var=y

Is this still happening?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.