/    Sign up×
Community /Pin to ProfileBookmark

Putting data from a user form into a URL

Hello,

Please can someone save me from insanity? I have been trying for days to figure out a way to do something that’s probably very simple to an expert.

I want to take text (e.g. a part number) from user input (on a form) and put it into a URL so when the user clicks ‘go’ he goes to the appropriate page. In essence I want to do something like this:

<INPUT NAME=”part” size=20>
<A HREF=” ‘http://www.mysite.com/partno-‘ + part + ‘.htm’ “>Go</a>

So if the user enters part number 12345 on the form he goes to [url]http://www.mysite.com/partno-12345.htm[/url]

Is it possible? If so how do I do it?


————————————————————————————–

This works in getting [url]http://www.mysite.com/partno-12345.htm[/url] into the URL variable and displaying it in an Alert:

<HTML><HEAD>

<script language=”JavaScript”>
function formHandler() {
var URL = ‘http://www.mysite.com/partno-‘+myform.part.value+’.htm‘;
alert(URL); }
</script>

</HEAD>

<BODY>

<form name=”myform”>
<p>Please enter part number here:<br />
<INPUT NAME=”part” size=20 onChange=”formHandler()”><br />
<INPUT TYPE=”submit” value=”GO”>
</form>

</BODY>
</HTML>

BUT when [B]alert(URL);[/B] is replaced by [B]location.href(URL);[/B] it does not work.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisDec 21.2007 — You might have better success with this:
location.href = URL;
Copy linkTweet thisAlerts:
@mikehrauthorDec 21.2007 — Gil, thank you but that gives the same result. It shows this in the address line:

http://www.mysite.com/part-finder.htm?part=12345

i.e it just adds part=12345 to the URL of the page that contains the code.
Copy linkTweet thisAlerts:
@gil_davisDec 21.2007 — <i>
</i>&lt;script type="text/javascript"&gt;
function formHandler() {
if (document.myform.part.value != "")
{window.location.href = "http://www.mysite.com/partno-" + document.myform.part.value + ".htm";}
else
{alert("Please enter a part number!");}
}
&lt;/script&gt;
&lt;form name="myform" onsubmit="return false"&gt;
&lt;input name="part" type="text" size=20&gt;
&lt;input type="button" value="Submit" onclick="formHandler()"&gt;
&lt;/form&gt;

There are other ways to skin the proverbial cat, but this should get you started.
Copy linkTweet thisAlerts:
@mikehrauthorDec 21.2007 — Gil,

Excellent! Works perfectly.

I really appreciate the trouble you took to do this piece of code for me - thank you.

Have a very Merry Christmas.

Mike
×

Success!

Help @mikehr 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 5.3,
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,
)...