Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Craig Watson
27,930 PointsExtract input vals on click, redirect to another page, input those vals into search field and automatically submit?
Hi everyone,
just looking for a little guidance on something here, on the home page I have an input filed and submit button.
I want the user to enter keyword(for jobs) click search and it redirect to the correct page showing the results.
I assume what I need to do is extract vals from first input, store them and once new page is loaded automatically enter them into search box on that page and again automatically click the search button so that all goes well.
you can see a working example of this type of thing here
type search in on home page show results on the correctly formatted page :)
any and all help is greatly appreciated!
I have listed this under javascript but there may be a solution in php as im a WordPress install im not sure....
Craig
3 Answers
Steven Parker
243,656 PointsJavascript would imply you're doing this on the client side. The trick is when you go to a different page, you lose the javascript that was running on the previous page. So to pass the information across from one page to another a few options come to mind:
- Attach the info to the URL like the folks at inautomotive are doing.
- Store the info in a cookie and retrieve it on the next page.
- Store the info into session local storage and retrieve it on the next page.
- Don't actually change pages. Have a master page that updates (via AJAX) a content region inside it.
- Have the server construct the new page with defaults based on the info posted back from the first page.
Sergio Alen
26,726 PointsYeah I'd go with Steven's first option:
Just like InAutomovite when someone enters keyword "Leader" submit the search form and pass the keyword param using a URL query string like this: /jobs?keyword=leader
then in the jobs page use the GET method to retrieve the value of the query string using php global variables like this: $_SERVER["QUERY_STRING"]
Store this $value in a variable then do a database query that selects all from jobs table where the description or title contains $value
You can actually use JS for this as well to retrieve the value of the query string with this function:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var jobKeyword = getParameterByName('keyword');
but to connect to the database you'll have to use AJAX
Hope that makes sense
sizwengubane
15,244 PointsHello guys, On 18th of January i am going to start a social networking website so i am looking for some talented front end and back end developers
my email is lakindu1.jayathilaka@gmail.com contact me if u are interested, there are 2 people in my team for now
Craig Watson
27,930 PointsCraig Watson
27,930 PointsHi Steve, thanks for your answer.
I have managed to get the page redirected when the button is clicked as well as the value from the search field added to the url as a query string.
Should I then use php to query the string as it seems the easier way and im using WordPress so can easily drop a little function in no problem :)
Craig