Jump to content

[SOLVED] Difficulty using GET_method


zwkle

Recommended Posts

Hi,

 

I have a search box on my site. Currently I have used the POST method, but I want to be able to have links on my site that execute searches when clicked, so I think I need to use the GET method. I have created the following test pages to explain my problem:

 

First I have the search box (using the POST method):

 

<form action='search.php' method='post'>

<input type="text" name="searchstring" size="50">

<input type="submit" value="Search">

 

And then I have the file search.php

 

<?php

$searchstring=$_POST['searchstring'];

echo $searchstring;

?>

 

This outputs the searched for string as expected.

 

Now, if I substitute POST for GET, first creating the page:

 

<form action='search.php' method='get'>

<input type="text" name="searchstring" size="50"?

<input type="submit" value="Search">

 

and changing the file search.php to

 

<?php

$searchstring=$_GET['searchstring'];

echo $searchstring;

?>

 

I find that the searched for string isn't outputted.

 

Can anyone tell me what is wrong? Basically my issue is that I don't know how to obtain the searchstring and use it in my code when I'm using the GET method.

 

Many thanks,

Andrew

 

 

Link to comment
https://forums.phpfreaks.com/topic/144089-solved-difficulty-using-get_method/
Share on other sites

It should work,

 

Are you doing any checking against the submit button at all?

 

 

but you could always try resetting the form to use post, and then use $searchstring=$_REQUEST['searchstring'] in your code. That should allow both post and get methods to send to the search.php script

If you use the GET method than the links are going to be preset.

 

$_GET is used for variables in the URL.

 

You would do something like this for GET:

 

Page with link:

$search_string = "underwear";
search underwear

 

search.php

$searchstring=$_GET['searchstring'];
echo $searchstring;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.