Jump to content


PHP Form: GET vs POST


15 replies to this topic

#1 Jshepp

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Canada
  • Interests:Creating websites, paintball, movies, hanging out.. you know, usual crap.

Posted 21 March 2008 - 09:54 PM

I'm just wondering if there would be a reason for GET method to not work, yet POST method will. Here's what I mean:

<?php echo $_GET["query"]; ?>
Comes up with simply nothing. Whereas

<?php echo $_POST["query"]; ?>
Comes up with what the value actually is. The form method is also changed, btw. Just showing the PHP..

#2 BigDog

    Young Padawan

  • Members
  • Pip
  • 277 posts
  • Gender:Male
  • Location:Orange County, California
  • Interests:Running, building computers, PC games and BMX and programming.

Posted 22 March 2008 - 02:24 AM

Well, if your form a post or get method?

#3 Jshepp

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Canada
  • Interests:Creating websites, paintball, movies, hanging out.. you know, usual crap.

Posted 22 March 2008 - 08:07 AM

View PostJshepp, on Mar 22 2008, 12:24 AM, said:

The form method is also changed, btw. Just showing the PHP..

I change it accordingly.

#4 BigDog

    Young Padawan

  • Members
  • Pip
  • 277 posts
  • Gender:Male
  • Location:Orange County, California
  • Interests:Running, building computers, PC games and BMX and programming.

Posted 22 March 2008 - 11:13 AM

Well, in your URL for the GET method, after pressing submit, do you see ?query="XXXXXXXXXX"?

#5 Jshepp

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Canada
  • Interests:Creating websites, paintball, movies, hanging out.. you know, usual crap.

Posted 24 March 2008 - 07:47 AM

Yes I do, and that's why I find it strange.

#6 BigDog

    Young Padawan

  • Members
  • Pip
  • 277 posts
  • Gender:Male
  • Location:Orange County, California
  • Interests:Running, building computers, PC games and BMX and programming.

Posted 24 March 2008 - 09:43 AM

Do you mind if you post the entire script?

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 March 2008 - 09:30 PM

die('<pre>'.htmlspecialchars(print_r($GLOBALS, 1)).'</pre>');
Put that in the code before your echo statment. It will show whether or not the get is even being recognized.

Are you using .htaccess redirection for anything here?

Edited by rc69, 24 March 2008 - 09:33 PM.


#8 Jshepp

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Canada
  • Interests:Creating websites, paintball, movies, hanging out.. you know, usual crap.

Posted 24 March 2008 - 09:56 PM

View Postrc69, on Mar 25 2008, 12:00 AM, said:

die('<pre>'.htmlspecialchars(print_r($GLOBALS, 1)).'</pre>');
Put that in the code before your echo statment. It will show whether or not the get is even being recognized.

Are you using .htaccess redirection for anything here?

Yes I am using .htacces redirection actually, and that's where my mind was wandering as well.

Script:
	  <h3>Database</h3>
	  <form id="search" method="GET" action="database">
		  <input type="hidden" name="formsent" value="1" />
	  <p>What would you like to search?</p>
	  <label for="query">Query:</label>
	  <input type="text" name="query" id="query" />
	  <input type="submit" value="Search" class="button"; />
	  </form>
	  
	   
 <?php
 
 if ($_GET['formsent'] == 1 )
	 {
		 //MySQL Connect
		 mysql_connect ('localhost','root','') OR die(mysql_error());
		 mysql_select_db ('skills') OR die(mysql_error());
		 
		 $query = $_GET["query"];
		 $select = "SELECT * FROM users WHERE first LIKE '$query%'" OR die(mysql_error());
		 $result = mysql_query ($select) OR die(mysql_error());
			  
	  echo '<table class="users">
		  <tr class="table_head">
			 <th>First Name</th><th>Last Name</th><th>Username</th><th>Password</th><th>email</th><th>Pic</th>
		 </tr>';
		 
 
		 while ($row = mysql_fetch_array($result))
			 {
				 $fn = $row[first];
				 $ln = $row[last];
				 $user = $row[user];
				 $pass = $row[pass];
				 $email = $row[email];
				 $pic = $row[pic];
 
 ?>
	 
		 <tr>
		 <td><?php echo "$fn" ?></td>
		 <td><?php echo "$ln" ?></td>
		 <td><?php echo "$user" ?></td>
		 <td><?php echo "$pass" ?></td>
		 <td><?php echo "$email" ?></td>
		 <td><?php echo "$pic" ?></td>
		 </tr>
		 
		 <?php } ?>
		 </table> <?php } ?>
There's probably umpteen better ways to do half the stuff I did there, but that's neither here nor there; it should be discernable.

Edited by Jshepp, 24 March 2008 - 09:57 PM.


#9 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 25 March 2008 - 06:57 AM

View Postrc69, on Mar 25 2008, 02:30 AM, said:

die('<pre>'.htmlspecialchars(print_r($GLOBALS, 1)).'</pre>');
Put that in the code before your echo statment. It will show whether or not the get is even being recognized.

Are you using .htaccess redirection for anything here?

Quick side not rc, you can use <xmp></xmp>, that way you don't have to do the htmlspecialchars on the value as it does it auto, not sure if you knew that or not :biggrin:

Back on topic...

can you post any htaccess that relates to this page please? I think i have an idea of what is going on.

#10 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 March 2008 - 08:43 PM

That's probably true, but i never did like seemingly non-standard tags, so i just do htmlspecialchars... Who know though, maybe knowing about xmp will come in handy one day, so thanks :)

Jshepp, I asked about the .htaccess because usually redirecting with it resets $_GET rather than appends to it (if you look at the address you redirect to, this should be seemingly obvious).

So, before posting the .htaccess file you have, try this out and see what happens.
$query = preg_replace('#(.*?)?#s', '', $_SERVER['REQUEST_URI']);
parse_str($query, $array);
$_GET = array_merge($_GET, $array);
unset($array, $query);

p.s. I know there is probably a more efficient way to do this, i just copied/pasted it and don't feel like rewriting it at the moment :biggrin:

#11 Jshepp

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Canada
  • Interests:Creating websites, paintball, movies, hanging out.. you know, usual crap.

Posted 26 March 2008 - 09:21 PM

rc69, I'm not quite sure what I'm supposed to do with that block of code.. :huh:

And here is my .htaccess:
RewriteEngine on
RewriteRule ^database(/?)$ index.php?id=database&page=database


#12 BigDog

    Young Padawan

  • Members
  • Pip
  • 277 posts
  • Gender:Male
  • Location:Orange County, California
  • Interests:Running, building computers, PC games and BMX and programming.

Posted 27 March 2008 - 12:11 AM

Try this:

RewriteEngine on
RewriteRule ^database/(.*)$ index.php?id=database&page=database&query=$1


#13 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 27 March 2008 - 01:23 AM

http://php.net/manua...n.parse-str.php
http://php.net/manua...array-merge.php

	  <h3>Database</h3>
	  <form id="search" method="GET" action="database">
		  <input type="hidden" name="formsent" value="1" />
	  <p>What would you like to search?</p>
	  <label for="query">Query:</label>
	  <input type="text" name="query" id="query" />
	  <input type="submit" value="Search" class="button"; />
	  </form>
	  
	   
<?php
/* Inserted here */
$query = preg_replace('#(.*?)?#s', '', $_SERVER['REQUEST_URI']);
parse_str($query, $array);
$_GET = array_merge($_GET, $array);
unset($array, $query);
/* Stopped inserting here */

if ($_GET['formsent'] == 1 )
/*
...
*/


#14 Jshepp

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Canada
  • Interests:Creating websites, paintball, movies, hanging out.. you know, usual crap.

Posted 27 March 2008 - 10:15 PM

View PostBigDog, on Mar 27 2008, 02:41 AM, said:

Try this:

RewriteEngine on
 RewriteRule ^database/(.*)$ index.php?id=database&page=database&query=$1
Nope, just 404's.

At rc69: That did it. Works now. Although I'm not really sure what that did.

Edited by Jshepp, 27 March 2008 - 10:16 PM.


#15 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 27 March 2008 - 11:34 PM

$_SERVER['REQUEST_URI'] returns the url of the page (ref:http://php.net/manual/en/reserved.variable...ariables.server) as defined in the address bar (not the redirected url).

The preg_replace() simply strips out the query string (which couldn't be accessed via $_SERVER['QUERY_STRING'] because of the redirect), and the parses it. Refer to the urls i provided in my previous post for more detailed info about the stuff after the call to preg_replace().

Edited by rc69, 27 March 2008 - 11:35 PM.


#16 Jshepp

    Young Padawan

  • Members
  • Pip
  • 11 posts
  • Gender:Male
  • Location:Canada
  • Interests:Creating websites, paintball, movies, hanging out.. you know, usual crap.

Posted 28 March 2008 - 03:39 PM

Ok, cool. Thanks a lot =)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users