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 ^^
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>
<? } ?>
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>
<?
}
?>
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.
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.
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>";
}
?>
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>");
}
?>
Here's more details on passing in form results as an array:
http://us.php.net/manual/en/faq.html.php#faq.html.arrays
Related
I want to have a simple HTML input field where people can type all kinds of nonsense. For example, a user types: "Hello, I'm Nicky". When the user then clicks the button Send, I want a simple PHP script to replace the word "Nicky" to "Nicki" and show it to the user. So basially, just a simple PHP script which replaces specific words from an input field and then print out the exact same line the user has inputted, except show Nicki instead of Nicky.
How can I achieve this, in the most simplest way?
My code looks like this now:
<?php
$_POST['name'] = str_replace("Nicky","Nicki",$_POST['name']);
?>
<form method="post">
<input type="text" name="name">
<input type="submit">
</form>
<?php
if(isset($_POST['form-action']) && $_POST['form-action'] == "submit-form"){ // form has been submitted
echo "<p>BEFORE: ".$_POST['name']."</p>"; // what the user entered "Nicky"
$_POST['name'] = str_replace("Nicky","Nicki",$_POST['name']); // find/replace Nicky with Nicki
echo "<p>AFTER: ".$_POST['name']."</p>"; // what the $_POST['name'] now is
}
?>
<form method="post">
<input type="text" name="name" value="Nicky">
<input type="submit">
<input type="hidden" name="form-action" value="submit-form">
</form>
In addition to this, if you want to expand the Find & Replace variables, you could use an array:
$FindReplace = array("Nicky"=>"Nicki", "Blue"=>"Red"); // build an array of find/replace variables
....
foreach($_POST as $Name=>$Value){
echo "<p>Before: ".$Name."=".$Value."</p>"
foreach($FindReplace as $Find=>$Replace){
$Value = str_replace($Find,$Replace,$Value);
}
echo "<p>After: ".$Name."=".$Value."</p>"
}
I am new to PHP.
I am trying to scrape a webpage and pull the value to my input box.
Scrapping is done, and stored in array,
<?php
function codeAddress1() {
$html = file_get_contents("http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=79401:006:0812");
preg_match_all('(<li.*?>.*?</li>)', $html, $matches);
var_dump($matches[0][0]);
$one=$matches[0][0];
var_dump($one);
}
?>
On button click i called function :
<input type="submit" CssClass="button" value="Search" width="35%" onclick="codeAddress1()">
Now i wanted to save the variable $one to my input box, i tried several, its not working.
like
<input id="t49" type="text" class="textbox" value="<?php echo #$one; ?>">
Any idea where i am going wrong.
Not quite sure what you want but
<li>(.*?)<\/li>
This will give you
<b>Maakond</b> : Tartu maakond
in $matches[1][0] and
<b>Omavalitsus</b> : Tartu vald
in $matches[1][1] etc. etc.
Can you please help me in this context.
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
</form>
I am using this code in a loop and I need to get all the values in another page.
How can I get these value as array in another page.
you should be able to use $_POST array in the sample.php page
PHP's $_POST Array, taken from the docs:
An associative array of variables passed to the current script via the HTTP POST method.
first, add s submit button to your form, it should look like this:
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
<input type="submit">
</form>
and in sample.php:
<?php
print_r($_POST);
?>
This is the most simple example,
once you get the hang of it, I recommend you read the docs on topics such as:
htmlspecialchars
htmlspecialchars — Convert special characters to HTML entities
You can access it as array
$_POST['first'][0] // 0 will be index for single
to list all just loop
foreach($_POST['first'] as $first) {
...
}
Your values will end up in $_POST, and because you're using the [] syntax, you'll get an array. So, to get your first array, do:
$first = $_POST['first'];
You could then iterate over it like so:
foreach ($first as $item) {
...
}
I think this will help you.
$first=$_POST['first'];
$second=$_POST['second'];
foreach($first as $key=>$first){
echo 'Your first value'.$first.'Your second value'.$second[$key];
}
Long story short, I want users to be able to call the value of the variable $city_name into a string that they will submit.
Here's my code;
<?php
if(!isset($post_text)) {
$city_name = "Dallas";
$post_text = $_POST['post_text'];
echo($post_text);
}
?>
<html>
<head></head>
<body>
<form action="" method="post">
<input name="post_text" type="text" value="enter text here" />
<input type="submit" value="submit" />
</form>
I was under the impression that calling $city_name in the form field post_text would return "Dallas", unfortunately that's not the case.
If anyone has any advice or information on what alterations I could add to this code in order to allow the user to call the value of $city_name, it would be greatly appreciated :)!!
Based on your comment:
I would like for the user to be able to input and submit a string such as "I am going to $city_name" and have "I am going to Dallas" echoed on submit.
This is the code:
$city_name = "Dallas";
$post_text = str_replace('$city_name', $city_name, $_POST['post_text']);
echo $post_text;
It's important to use single quotes around '$city_name' here as you are searching for the literal text $city_name and not the contents of the variable $city_name.
I am trying to add commenting like StackOverflow and Facebook uses to a site I'm building. Basically, each parent post will have its own child comments. I plan to implement the front-end with jQuery Ajax but I'm struggling with how to best tackle the PHP back-end.
Since having the same name and ID for each form field would cause validation errors (and then some, probably), I added the parent post's ID to each form field. Fields that will be passed are commentID, commentBody, commentAuthor - with the ID added they will be commentTitle-12, etc.
Since the $_POST array_key will be different each time a new post is processed, I need to trim off the -12 (or whatever the ID may be) from the $_POST key, leaving just commentTitle, commentBody, etc. and its associated value.
Example
$_POST['commentTitle-12']; //how it would be received after submission
$_POST['commentTitle']; //this is what I am aiming for
Many thanks
SOLUTION
Thanks to CFreak-
//Basic example, not actual script
<?php
if (array_key_exists("send", $_POST)) {
$title = $_POST['title'][0];
$body = $_POST['body'][0];
echo $title . ', ' . $body;
}
?>
<html>
<body>
<form name="test" id="test" method="post" action="">
<input type="text" name="title[]"/>
<input type="text" name="body[]"/>
<input type="submit" name="send" id="send"/>
</form>
</body>
</html>
Update 2
Oops, kind of forgot the whole point of it - unique names (although it's been established that 1) this isn't really necessary and 2) probably better, for this application, to do this using jQuery instead)
//Basic example, not actual script
<?php
if (array_key_exists("send", $_POST)) {
$id = $_POST['id'];
$title = $_POST['title'][$id];
$body = $_POST['body'][$id];
echo $title . ', ' . $body;
}
?>
<html>
<body>
<form name="test" id="test" method="post" action="">
<input type="text" name="title[<?php echo $row['id'];?>]"/>
<input type="text" name="body[<?php echo $row['id'];?>]"/>
<input type="hidden" name="id" value="<?php echo $row['id']; //the ID?>"/>
<input type="submit" name="send" id="send"/>
</form>
</body>
</html>
PHP has a little trick to get arrays or even multi-dimensional arrays out of an HTML form. In the HTML name your field like this:
<input type="text" name="commentTitle[12]" value="(whatever default value)" />
(you can use variables or whatever to put in the "12" if that's what you're doing, the key is the [ ] brackets.
Then in PHP you'll get:
$_POST['commentTitle'][12]
You could then just loop through the comments and grabbing each by the index ID.
You can also just leave it as empty square brackets in the HTML:
<input type="text" name="commentTitle[]" value="(whatever default value)" />
That will just make it an indexed array starting at 0, if you don't care what the actual ID value is.
Hope that helps.
You just have to iterate through $_POST and search for matching keys:
function extract_vars_from_post($arr) {
$result = array();
foreach ($arr as $key => $val) {
// $key looks like asdasd-12
if (preg_match('/([a-z]+)-\d+/', $key, $match)) {
$result[$match[1]] = $val;
} else {
$result[$key] = $val;
}
}
return $result;
}
Didn't test the code, though