Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP $_REQUEST as array

I have a search form, I want to $_REQUEST the search terms as an array so I can list each search term out, wrapping each term in a span for styling. How do I do that?

Edit: Here's the code requested.

<form action="http://localhost/wordpress" id="search" method="get">
<input type="text" size="30" id="s" name="s" value="Type and hit enter" onfocus="javascript:this.value='';" onblur="javascript:this.value='Type and hit enter';"/>
<br/>
<input type="submit" value="Search"/>
</form>

Update: Thanks guys for the responses. I'll use explode, it seems fairly straightforward. Plus the name sounds cool ^^


2 Answers

In the form:

<input type="text" name="terms[]" />
<input type="text" name="terms[]" />
<input type="text" name="terms[]" />

In the form processor:

<? foreach($_REQUEST['terms'] as $term) { ?>
    <span style="searchterm"><?= htmlspecialchars($term) ?></span>
<? } ?>
like image 109
chaos Avatar answered Sep 10 '25 03:09

chaos


If you want the user to enter multiple search terms in separate input controls, the above answers should be helpful. However, your example form leads me to wonder if you want to use only one search phrase input text box. If that's so, this might be what you're looking for:

<?php
  $searchTerms = preg_split("/[\s,]+/", $_REQUEST['SearchTerms']);

   foreach($searchTerms as $term) { ?>
     <span class="term"><?= htmlentities($term) ?></span>
<? 
   }
?>
like image 20
Ben Gribaudo Avatar answered Sep 10 '25 02:09

Ben Gribaudo