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 it because I need to output what the user typed. And the message, because I need to access the mysql database in relation to this 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 cause the mysql content to be displayed depending on what the user checks:

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

And the action of the form of these two together 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 a lot of undefined errors when I do this. What can you recommend me to do to get the value entered by the user along with the mysql data.

+7
php mysql
source share
5 answers

You can only have a verb (POST, GET, PUT, ...) when making an HTTP request . However you can do

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

and then PHP will populate $_GET['foo'] , although the submitted request was POST'ed.

However, your problem seems to be much larger than what you are trying to submit two forms at once , aimed at two different scenarios. This is not possible within a single request.

+45
source share

You cannot perform GET and POST at the same time.

Combine two forms into one.

For example, combine the forms into a single post form. In your code, extract everything you need from $ _POST.

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

+4
source share

Like other answers, you cannot receive a request for receiving and sending at the same time. But if you want to unify your PHP code to read the variable obtained through a request to receive or publish, perhaps you can use $ _ REQUEST

+3
source share

POST and GET (like HEAD, FILE, DELETE, etc.) are HTTP methods . Your browser sends an HTTP request to the server with one of them before the request, so you cannot send two methods at the same time (example 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 receives a response from url and POST also sends some content data to this URL.When you submit your form, the data is collected in the standard format defined by the enctype attribute and also sent this time on the URL.

Also the url is formatted in a standard way and the part of the string found behind? The character is called QUERY STRING .

When the server receives data, it passes this information to PHP, which reads the URL and reads the method, request body (data) and a huge amount of other things. Finally, it fills its superglobal arrays with this data so you know what the user is sending ( $ _ SERVER , $ _ GET and $ _ POST and many others);

Important notice! In addition, if PHP fills the $ _GET superglobal sum with a URL query string and, ultimately, the $ _POST super switch with the data found in the request body, $ _ POST and $ _GET are not related to HTTP .

So, if you want to fill out $ _ POST and $ _ GET at the same time, you should submit your form as follows:

 <form method="post" action="http://myurl/index.php?mygetvar=1&mygetvar=2"> <input name="year" type="text" /> <imput type="submit" /> </form> 
+3
source share

I have not tested this, but it is a possible solution to submit GET variables via the form form 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]; } } 

This is not entirely flawless proof, but I ran into a problem with the header error ("Location:"), which included getting variables that the server does not see under $ _GET with a URL, for example http://website.com/page ? msg = 0 , but they existed in the variable $ _SERVER ['REQUEST_URI']. Hope this helps and good luck!

+2
source share

All Articles