Jump to content

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;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.