Reputation:
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 ^^
Upvotes: 4
Views: 38379
Reputation: 543
I did it this way..
Passing an array (it's really just a string to PHP):
http://www.somesite.net/myscript.php?myArray=Planes,Trains,Automobiles
Then in the script, just explode the string:
$myArray = explode(",", $_REQUEST['myArray']);
Maybe not exactly what you're looking for.
Upvotes: 3
Reputation: 27765
If you want break your search terms by space symbols just try this code:
<?php
$search_terms = explode(" ", $_REQUEST['s']);
foreach($search_terms AS $search_term_item) {
echo "<span class=\"SearchTerm\">".htmlspecialchars($search_term_item)."</span>";
}
?>
Upvotes: 1
Reputation: 5147
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>
<?
}
?>
Upvotes: 3
Reputation: 249
I figure you wish the user to have one single entry input, which you then wish to split into an array of separate search terms.
Split your input on whitespace (treating consecutive whitespace characters as one) to derive separate terms.
For example :
$termList = preg_split("/\s+/", trim($_REQUEST['s']));
foreach($termList as $term) { echo "<span>".htmlspecialchars($term)."</span>\n"; }
Ofcourse do not forget to properly filter and escape the input before you use it.
Upvotes: 1
Reputation: 5147
Here's more details on passing in form results as an array: http://us.php.net/manual/en/faq.html.php#faq.html.arrays
Upvotes: 0
Reputation: 52508
In your HTML form element you can assign the name to an array, like this:
<select id="MySelect" multiple="multiple" name="SearchTerms[]" class="MyClass">
...
</select>
then when you deal with the form after submittion you can do something like:
<?php
foreach($_REQUEST['SearchTerms'] as $SearchTerm)
{
Print("<span class=\"SearchTerm\">$SearchTerm</span>");
}
?>
Upvotes: 0
Reputation: 124307
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>
<? } ?>
Upvotes: 6