Scrape data to input box in PHP - php

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.

Related

Change certain text from User input (From a User Input field)

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>"
}

confused about basic find and replace app

I am trying to do a find and replace applicaiton the problem is that after cliked submit button all the text fields gets clean nothing displays on the screen What am i doing wrong
<?php
$offset=0;
if(isset($_POST['text'] ) && isset($_POST['searchfor']) && isset($_POST['replacewith'])){
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$searchLength=strlen($search);
if(!empty($text) && !empty($search) &&!empty($replace)){
while ($strpos= strpos($text,$search,$offset)){
echo $offset=$strpos+$searchLength;
}
} else {
echo "<script>alert('errorrrr')</script>";
}
}
?>
<form action="#" method="post">
<textarea name="text" id="" cols="30" rows="10"></textarea><br>
Search For:<br>
<input type="text" name="searchfor"><br>
ReplaceWith<br>
<input type="text"name="replacewith"><br>
<input type="submit" value="Fr..."></>
</form>
Regarding your form, you decided to submit to the same page.
Doing this, the page is obviously fully reloaded when submitted. Hence it is normal that what you typed in has disappeared.
If you want to see it again, you have to display you variables in the HTML code.
For example:
<?php
$myVar = "";
if(isset($_POST['myVar']){
$myVar = $_POST['myVar'];
}
?>
<form>
<input type="text" value="<?php echo $myVar;?>"/>
</form>
NB: I encourage you to filter the user entry.
Regards
there is problems in your code :
1 - echo $offset=$strpos+$searchLength; the echo can't be used in this format. insted use echo $offset; in next line for seeing offset values.
2 - if the text be like 'amir love persepolis' and search for 'amir' to replace it with 'all men's' you will have another issue, because you will have while ( 0 ) situation. think about this too!

How to pass a GET Variable to a form

I have three pages. One of which there is a list of texts the user can select. Upon clicking on one of the texts they will be redirected to another page by using:
<a href='second.php?text=whatever>Whatever</a>
A page where they will input the username they wish to send those texts to - using forms. I wish to proceed to the third page with those two variable - texts and username. I only manage to proceed to third page with username only.
I am getting third.php?username=inputtedUsername.
I want to get third.php?username=inputtedUsername&&text=whatever.
I am aware that I can do by storing the text to a SESSION on page two and than transfer it over to third page.
I wish to know if there is another secure way to do this - maybe something needed to be changed in the form action=thirdpage.php? I dont know. Thank you. ö.ö.
Solved: After reading comments and answer, the thing I need was type=hidden. It is now working on my part. Thanks everyone for helping me. :).
'second.php?text=whatever'? You can't just put whatever to the text, you are doing it wrong. Try this.
firstpage.php
<?php
$whatever = 'Tom & Jerry, 1 + 2 = 3';
echo '' . $whatever . '';
?>
secondpage.php
<form action="thirdpage.php" method="post">
<input type="text" name="username" value="" />
<input type="hidden" name="text" value="<?php echo base64_decode($_GET['text']); ?>" />
<input type="submit" value="Submit" />
</form>
thirdpage.php
<?php
echo 'Username: ' . $_POST['username'];
echo '<br />';
echo 'Text: ' . $_POST['text'];
?>

Form that completes Values in PHP

I have a normal form in a PHP page, I send data to the php page from another for using POST. The PHP page runs some scripts to update data to SQL but on that page I have a second form that needs to be completed with data from the initial form prior to updating the SQL.
$recipient_nr = $_REQUEST['recipient_nr'];
Here I draw the info from the first form
Now I want to use this in a new form on the current PHP page how do I state this in the new form
<input type="text" name="recipient_nr" id="recipient_nr" value=".$recipient_nr.">
This is what I am attempting but it is not working I know I have too many "'" xxx"'" in the lines but not sure how to remidy this
Do you generate the new form in PHP? If so, where is the code where you do that?
If this is some kind of ...?> <input type="..."...> <?php ... page generation then you'll need to echo that $recipient_nr into the PHP-generated response:
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?php echo $recipient_nr; ?>">
<?php
...
Or, if you have short echos turned on,
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?= $recipient_nr ?>">
<?php
...
use this:
<input type="text" name="recipient_nr" id="recipient_nr" value="<?php echo $recipient_nr; ?>">
Something like this:
$recipient_nr = array_key_exists('recipient_nr', $_REQUEST)
? (string) $_REQUEST['recipient_nr']
: 'default value or empty string';
And output it:
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?=$recipient_nr?>">
But if you want store this data for/between other pages you can use $_SESSION global array for save it.

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 ^^
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

Categories