9

Do you have any suggestions with my problem. I need to use get and post at the same time. Get because I need to output what the user has typed. And post because I need to access the mysql database in relation to that input. It looks something like this:

<form name="x" method="get" action="x.php">
<input name="year" type="text">

<select name="general" id="general">
        <font size="3">
        <option value="YEAR">Year</option>

</form>

This will output the contents of mysql depending on what the user will check:

<form name="y" method="post" action"y.php">
<input name="fname" type="checkbox">
</form>

And the form action of those two combined will look something like this:

   <?php

               if($_POST['general'] == 'YEAR'){
                   ?>
                   <?php echo $_GET["year"]; ?>
                   <?php
            $result2 = mysql_query("SELECT * FROM student
    WHERE student.YEAR='$syear'");
    ?>
    <table border='1'>
            <tr>

                    <?php if ( $ShowLastName ) { ?><th>LASTNAME</th><?php } ?>
                    <?php if ( $ShowFirstName ) { ?><th>FIRSTNAME</th><?php } ?>
        </tr>

    <?php while ( $row = mysql_fetch_array($result2) ) {
        if (!$result2)  { 

    }
        ?>
            <tr> 
                    <td><?php echo $row['IDNO']?> </td>
                    <td><?php echo $row['YEAR'] ?> </td>
     <?php if ( $ShowLastName ) { echo('<td>'.$row['LASTNAME'].'</td>'); } ?></td>
                    <?php if ( $ShowFirstName ) { echo('<td>'.$row['FIRSTNAME'].'</td>'); } ?>

I really get lots of undefined errors when I do this. What can you recommend that I should do in order to get the value inputted by the user together with the mysql data.

user225269
  • 10,743
  • 69
  • 174
  • 251
  • Why, exactly, do you need to use both `$_GET` and `$_POST` simultaneously? The reasons you've given so far don't make sense. – outis May 01 '10 at 10:19
  • actually its just my idea. I'm asking what are the alternatives to achieve the same thing – user225269 May 01 '10 at 11:22
  • could you please finally review the answers to this and your previous questions and either accept the most helpful ones or edit your questions to point out why none of the answers solve your question. Thanks. – Gordon Nov 01 '10 at 16:54

6 Answers6

58

You can only have one verb (POST, GET, PUT, ...) when doing an HTTP Request. However, you can do

<form name="y" method="post" action="y.php?foo=bar">

and then PHP will populate $_GET['foo'] as well, although the sent Request was POST'ed.

However, your problem seems to be much more that you are trying to send two forms at once, directed at two different scripts. That is impossible within one Request.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Maybe with some JavaScript we can modify the action before submit. Man, I was looking for this, thanks! – kazinix Jul 15 '11 at 01:16
  • Is this a good practice? I was thinking about making this for a Web Service REST api, to send the method name by ``$_GET`` and specific method params as ``$_POST`` but I wanted to know if there's any recommendations about doing this – Juan Carlos Alpizar Chinchilla Jul 03 '15 at 10:53
  • @JuanCarlosAlpizarChinchilla not sure what you mean by "send the method name". REST API usually dont send method names. I am not aware of a recommendation for or against the technique above outside what is given in the link about verbs already. – Gordon Jul 03 '15 at 11:02
  • @Gordon for example, make a request to ``api.php?method=GetAuth`` (or maybe rename it to api.php/GetAuth with rewrite rules if possible) and then send the proper auth data via ``$_POST``, that would populate both arrays but I wasn't sure if that is a good practice – Juan Carlos Alpizar Chinchilla Jul 03 '15 at 11:07
  • 1
    @JuanCarlosAlpizarChinchilla in REST you usually model your URLs around resources and use the http verbs to do state transitions, e.g. `GET|POST|DELETE example.com/auth` – Gordon Jul 03 '15 at 13:02
5

You cannot do a GET and POST at the same time.

Combine the two forms into one.

For example combine the forms to one 'post' form. In your code extract whatever you need from $_POST.

And 'YEAR' does not equal 'Year', your sample code also needs work.

zaf
  • 22,776
  • 12
  • 65
  • 95
4

As saig by the other answers, you can't do a get and a post request at the same time. But if you want to unify your PHP code in order to read a variable received through a get or post request, maybe you could use $_REQUEST

David Morales
  • 17,816
  • 12
  • 77
  • 105
3

POST and GET (as HEAD, FILE, DELETE etc.) are HTTP methods. Your browser send an HTTP request to the server with one of them in front of the request so you cannot sent two method at the same time (an example of the request header from a web sniffer):

GET / HTTP/1.1[CRLF]
Host: web-sniffer.net[CRLF]
Connection: close[CRLF]
User-Agent: Web-sniffer/1.0.31 (+http://web-sniffer.net/)[CRLF]
Accept-Encoding: gzip[CRLF]
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
Cache-Control: no[CRLF]
Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]
Referer: http://web-sniffer.net/[CRLF]

The big difference from GET and POST is that GET retrieve a response from an url and POST send also some content data to that url. When you submit your form, data is collected in a standard format defined by enctype attribute and sent, also this time, to an url.

Also the url is formatted in a standard manner and the portion of string found behind the ? character is called QUERY STRING.

When the server receives data, it communicates these informations to PHP which reads the URL and reads the method, the body (data) of the request and a huge amount of other things. Finally it fills its superglobal arrays with this data to let you know what the user sends ($_SERVER, $_GET and $_POST and a bunch of others);

Important Notice! Also if PHP fill the $_GET superglobal with the query string of the URL and eventually the $_POST superglobal with the data found in the request body, $_POST and $_GET are not related to HTTP.

So, if you want fill $_POST and $_GET in the same time you must send your form in this manner:

<form method="post" action="http://myurl/index.php?mygetvar=1&mygetvar=2">
    <input name="year" type="text" />
    <imput type="submit" />
</form>
yuri
  • 575
  • 1
  • 14
  • 33
2

I haven't tested this but it's a possible solution for sending GET variables through a form's action value...

$request_uri = $_SERVER['REQUEST_URI'];
$request_uri = str_replace("&", "?", $request_uri);
$request_args = explode("?", $request_uri);
foreach($request_args as $key => $val) {
    if(strpos($val, "=") > 0) {
        $nvp_temp = explode("=", $val);
        $_GET[$nvp_temp[0]] = $nvp_temp[1];
    }
}

It's not completely fool proof, but I ran across an issue with a header("Location:") bug earlier that included get variables not being seen by the server under $_GET with a url such as http://website.com/page?msg=0 but they existed in the $_SERVER['REQUEST_URI'] variable. Hope this helps and good luck!

MLK.DEV
  • 453
  • 7
  • 31
0

You can also use a hidden field in the form

<input type="hidden" id="whatever" name="foo" value="bar">

and use

$_POST['foo']

to retrieve the value.

ztyreg
  • 139
  • 2
  • 4