user225269
user225269

Reputation: 10913

Post and get at the same time in php

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.

Upvotes: 9

Views: 40171

Answers (6)

ztyreg
ztyreg

Reputation: 179

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.

Upvotes: 0

Gordon
Gordon

Reputation: 317129

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.

Upvotes: 60

MLK.DEV
MLK.DEV

Reputation: 453

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!

Upvotes: 2

yuri
yuri

Reputation: 585

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>

Upvotes: 3

David Morales
David Morales

Reputation: 18064

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

Upvotes: 4

zaf
zaf

Reputation: 23264

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.

Upvotes: 4

Related Questions