isakhejnesen Posted June 17, 2013 Share Posted June 17, 2013 Hi there. I have a small problem, i want to use strrpos and i dont want to make a textbox in the browser so i can write what ever word i want to search for and enter it. here is the code. $string = "Dette er en dummy tekst. Den kan bruges til mange forskellige ting.<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam pellentesque faucibus pretium. Duis vehicula lacus vitae lacus adipiscing dignissim. Suspendisse mauris nulla, vehicula id orci semper, iaculis tincidunt nisl. Quisque congue magna eu elit posuere lacinia eu eget neque. Vivamus est sapien, porta at lacinia eget, commodo vitae lectus. Donec non velit at justo viverra suscipit eu quis purus. Integer tempor lacinia turpis, et aliquet est dignissim non. Aenean euismod dignissim turpis id sollicitudin. Phasellus vitae iaculis orci. Phasellus luctus faucibus tortor vitae porta. Proin semper tristique erat. Suspendisse ac fringilla elit, iaculis condimentum leo. Aliquam sit amet massa non dui congue laoreet eget et libero. Integer viverra nisi a justo tempor sollicitudin. Etiam volutpat aliquet malesuada. Ut erat neque, adipiscing sed dictum ut, commodo vel lacus.</p>"; echo '<h1>Strpos test</h1>'; echo ' <form> <input type="text" placeholder="Search" name="textbox" /> <input type="submit" name="searchButton" value="search" /> </form> '; if(isset($_POST['searchButton'])){ $textbox = $_POST['textbox']; }else{ echo 'type word to search '; } echo '<br />Your result is number '; echo strrpos($string, "word"); But i get the error that i have a Undefined variable: textbox in line 29 How can i make it so that what ever i enter in the textbox form will be the word that is searches for heres a link if you don't know what strrpos is. http://www.w3schools.com/php/func_string_strrpos.asp Quote Link to comment https://forums.phpfreaks.com/topic/279273-help-with-strrpos/ Share on other sites More sharing options...
Psycho Posted June 17, 2013 Share Posted June 17, 2013 (edited) Your form is not validly defined. You should be setting the method at the very least.If line 29 is the one right after the if() statement you should be getting an error about an undefined INDEX, I don't know why you would be getting that error. If searchButton is in the POST data, I would assume 'textbox' is too. Maybe there is some code you are not sharing that is causing this. But, here's an easy fix - why do you care if 'searchButton' is in the POST data? You just need to check for 'textbox'. Besides, button fields are only passed if the user clicks the button! So, your page would fail to process if the user pressed enter on their keyboard. So, you should just check the REQUEST_METHOD to see if the form was POSTed or, in this simple case, you can just check that field. However, you state the error is about an undefined VARIABLE "textbox", but the only use of such a variable in what you provided is where you attempt to set the value. That would not cause that error. But, what you posted doesn't make sense. You attempt to define a variable $textbox, but then later use a variable called $string. Now, if you are using $textbox on that line (and not $string) it would cause that warning when the form is not submitted since the variable would not be set. if(isset($_POST['searchButton'])) { $textbox = trim($_POST['textbox']); echo '<br />Your result is number '; echo strrpos($textbox, "word"); } else { echo 'type word to search '; } Edited June 17, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/279273-help-with-strrpos/#findComment-1436452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.