/    Sign up×
Community /Pin to ProfileBookmark

search box to open links

Hi everyone.
I need to put a search bar into a website that opens a new link dependant on what is typed into the search bar.

Users will be entering 1 of 50 keywords into this search bar and I want a different url to open depending on what has been entered.
Ie. If ‘one’ is input, I want the page ‘one.htm’ to open, if ‘two’ is entered then ‘two.htm’ opens and so on.

I know that this can be down easily from a dropdown list, but space is an issue so I need to complete this selection by a typeable search bar!

Can anyone point me in the right direction to get this started?

Thanks in advance.
Glen

to post a comment
HTML

12 Comments(s)

Copy linkTweet thisAlerts:
@jimmygMar 26.2011 — Will your search bar have a search button?
Copy linkTweet thisAlerts:
@glengatesauthorMar 26.2011 — HI jimmyg. Yes it should.
Copy linkTweet thisAlerts:
@jimmygMar 26.2011 — Ok cool, I have an idea for a way to do this put it involves using php and is a little unwieldy.

Would php be ok?
Copy linkTweet thisAlerts:
@glengatesauthorMar 27.2011 — Yes, any help to get it to work would be greatly appreciated.
Copy linkTweet thisAlerts:
@jimmygMar 27.2011 — Ok,

First of all you need to set up a HTML form which will call a PHP script when search is clicked. Say something like
<i>
</i> &lt;form enctype="multipart/form-data" action="link.php" method="POST"&gt;
&lt;input type="text" name="text" /&gt;
&lt;input type="submit" value="Search" /&gt;

&lt;/form&gt;

This will call a script called link.php when Search is clicked.

As for the php script it'll be something like this,
<i>
</i>//Turn the content from text box into variable
$page=$_POST['text'];

//set up a redirect to that page
header("location: $page.html");


This code should read the variable from the text box e.g. "one" and then use the redirect to open the page using that variable i.e $page.html = one.html.

There are probably better ways to do it but this might help to get you started
Copy linkTweet thisAlerts:
@glengatesauthorMar 27.2011 — hi jimmyg,

Thanks for your help!

I am a total newbie and have never used php before so have tried what you posted below, but when i enter text into the searchbar and click search, it opens/downloads the php script.

Dont know whether im doing it wrong!

Is there any chance you can type out what the script would look like if i wanted the searchbar to open the following links as an example?

ie. if i type 'one' it opens www.google.com

'two' opens bing.com

Thanks again for your help.

Glen
Copy linkTweet thisAlerts:
@jimmygMar 27.2011 — ok heres what I have so far

link.html
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form enctype="multipart/form-data" action="link.php" method="POST"&gt;
&lt;input type="text" name="text" /&gt;
&lt;input type="submit" value="Search" /&gt;

&lt;/form&gt;


&lt;/body&gt;
&lt;/html&gt;


This is just a simple html page I set up on my wamp server. When someone types google into the search box it will call this script

link.php (this is a seperate page)
<i>
</i>&lt;?php
//Turn the content from text box into variable
$page=$_POST['text'];

//set up a redirect to that page
header("location: http://$page.com");
?&gt;


Typing google or bing into the search box will open google.com or bing.com.

or if you want the user to type in "one" or "two" for a specific site try this
<i>
</i>&lt;?php
//Turn the content from text box into variable
$page=$_POST['text'];

if($_POST['text']=='one') {
//set up a redirect to that page
echo "&lt;meta http-equiv="refresh" content="0;URL=http://www.google.com"&gt;";

}

else if($_POST['text']=='two') {

echo "&lt;meta http-equiv="refresh" content="0;URL=http://www.bing.com."&gt;";

}



?&gt;


That should work. bear in mind link.php is an entirely seperate page.
Copy linkTweet thisAlerts:
@glengatesauthorMar 27.2011 — hi jimmyg

thanks for your help. I do appreciate it.

When i use the bottom script and type 'one' or 'two', im faced with the option of opening or saving link.php instead of directing to google or bing

Am i doing something badly wrong?

Glen
Copy linkTweet thisAlerts:
@jimmygMar 27.2011 — Ok do you have the site online anywhere or are you just using it on your home machine?
Copy linkTweet thisAlerts:
@glengatesauthorMar 27.2011 — Sorry, its on my home pc.

Should the searchbar try and open or download the php file or just reference it?

I copied exactly what you had sent.

Do you have any other suggestions as i sense what i need is so close?

Thanks

Glen
Copy linkTweet thisAlerts:
@jimmygMar 27.2011 — It sounds to me that you need to install php on your pc. I recommend WAMP server. This will set your PC up as a local server. Its what I use whenever im doing webwork. Its also open source so its free [code=html]http://www.wampserver.com/en/[/code]. Very easy to use as well
Copy linkTweet thisAlerts:
@sallar123Nov 07.2019 — hello jimmy i also had the same problem but i followed your steps and have successfully run it, but I have a query, can you please tell me if i write any wrong text, meaning a text that is not concerned in my website so what code should i write in "else" of my php code? what if i want to refresh a page when someone searches something that isn't avail in my page, can you provide me an "else" code for refreshing my current page? thanks in advance!
×

Success!

Help @glengates spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...