user2905014
user2905014

Reputation: 23

Search Function/Quick Search: How to get HTML Input value to php $variable without $_POST or $_GET

I have a quick search tab at the top of my menubar. It has an input textbox (for the content to search for) and a button. When this user currently fills in box and clicks the button the user is brought to the actual search page.

On this search page, a more advanaced set of search filters is displayed at the top of the page (with a submit button) and then the results of the search would be displayed below.

What I want to do is display the results of the basic search (from any other page, using the menubar) upon the search page loading without the user having pressed the submit button on that page. This will hopefully save time for the user (maybe the advanced search wasn't needed). NOTE: The advanced filter textbox regarding what text to search for is autopopulated by the content from the basic search textbox when the basic search button is clicked and the user is brought to the search page.

However, since I am not pressing the submit button I can't use the $_POST["searchText"] as a parameter for my if condition (which only displays search results if the user entered text in the textbox) to display the search results. Instead I would much rather user something like:

!is_null(searchText.value)

But obviously this is incorrect syntax/doesn't exist. Does anyone have a suggested solution to get the content from the HTML Input searchText into a PHP $variable?

Upvotes: 0

Views: 2699

Answers (3)

artur99
artur99

Reputation: 818

$_REQUEST["name"] Replaces both POST and GET

<form action="#" method="POST">
<input type="text" name="one">
<input type="submit" />
</form>
<form action="#" method="GET">
<input type="text" name="one">
<input type="submit" />
</form>

<?php
if(isset($_REQUEST["one"])){echo $_REQUEST["one"];}
?>

Upvotes: 0

Jerbot
Jerbot

Reputation: 1168

So on your advanced search page, you are looking at doing the following:

<input type='text' name='searchText' value='<?php echo (isset() ? htmlentities($_POST['searchText'], ENT_QUOTES) : ''; ?>' />

I think htmlentities is correct there, but I may be wrong.

The echo is actually a ternary. I believe isset() is probably the equivalent you are looking for. Basically, it checks if $_POST['searchText'] is set, and either echoes the contents of that, or nothing at all.

$_POST and $_GET are global variables, so you can get away with just checking them with isset(). However, for your own arrays, you need to check with array_key_exists('key', $array) before checking with isset($array['key']).

Upvotes: 0

Mike Brant
Mike Brant

Reputation: 71422

I don't quite follow why POST/GET could not be used. When the user is on page X and they enter a search value into the menubar, they would be taken to the search page with the search query passed via GET/POST.

It is really up to you to change your server-side logic to immediately perform the search on this query and show the results rather than show the more advanced search filter options (or to show the advanced options in addition to the search result). You don't need a second communication from the search page to the server to initiate the search functionality.

Upvotes: 1

Related Questions