Solved

Create a URL link based on input text

Posted on 2004-09-22
17
722 Views
Last Modified: 2012-06-27
Hey Gang,

Hopefully this is really simple.  I would like to place a text box on my page which, after some text is entered, that text is inserted into a URL, and the link is submitted.

I.E.  A user enters 1234 into a text box and clicks the "Submit" button, and is sent to "http://www.thispage.com/1234.htm"

Doesn't matter if this is done by html, dhtml, Javascript...whatever :-)

Thanks in advance!
Mike
0
Comment
Question by:Mike R.
  • 9
  • 6
  • 2
17 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
Comment Utility
Hi rightmirem,

<form name="myForm" action="" method="post">
<input type="text" name="myURL" size="20" />
<input type="submit" value="Submit" onclick="this.form.action='http://www.thispage.com/'+this.form.myURL.value;" />
</form>

Regards,
Zyloch
0
 
LVL 3

Author Comment

by:Mike R.
Comment Utility
Tha did it...thanks! (I new it would be simple!  This is what happens when you have Net Admins doing web pages :-)
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Heh, you need any more help, just post here. I know a lot of great EE people that're bound to help you
0
 
LVL 3

Author Comment

by:Mike R.
Comment Utility
Hey...since I have your ear...two more quick things related to this form.

1. I noticed, when the enter key is pressed (as opposed to the "Sunbmit" button) it does not go to the correct link (appears not to form the URL right).  Is there a way to fix this?

2. Since the form opens up a new web page based on the input...is there a way to have the current page verify that the new page exists before loading it and, in the event it does NOT exist, go to a default page?

Thanks again!
M
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Ok. Let's see. For #1, that's my mistake.

Take the code out of the onclick in the button, and move it into onsubmit in the <form> tag taking out the 'form'

As for #2, I believe there is a way with xmlHTTP and Javascript that's supported by most recent browsers. Try this: (not tested)

function testPageExist(page) {
   var xhttp=(document.all) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
   xhttp.open("GET",page,false);
   try {
      if (document.all) {
         xhttp.send();
      } else {
         xhttp.send(null);
      }
   } catch(e) {
      return false;
   }
   return true;
}


You would have this in the onsubmit of the form:

<form onsubmit="if (testPageExist(this.myURL.value){this.action='http://www.thispage.com/'+this.myURL.value;}else{return false;}">
0
 
LVL 3

Author Comment

by:Mike R.
Comment Utility
The "onsubmit" works great (I took out the testPage part for the first step)...but I am not sure how to add the xml to the page.  If I save the page as XML (using Frontpage) the "XML veiw" checker comes up with a bunch of errors (as this form is part of an existing webpage.)

Can I add the xml code to an existing HTML document and, if so, how?

Thanks!
Mike
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Nope, nope, don't save it as HTML. Put the function in your <script> tag on your page :)
0
 
LVL 3

Author Comment

by:Mike R.
Comment Utility
Well, heres the problem..and if resolving this does not appear to be simple, I think I may just scrap it...

I seem to have gotten everything to work with the script EXCEPT, I actually need to verify a page by it's full URL...as this is not a page on MY site.  Basically, everything seems to be working if the "page" in "testPageExist(page)" is local to my site...but if "page"="www.differentSite.com/index.html" (or any variation, "http://www.differentSite.com/index.html", "123.123.123.123/index.com", ETC.) it fails.

Is it a syntax issue...or can this just not be done?

Thanks!
M
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
It works on my computer with IE6. However, once SP2 is uploaded, and Mozilla right now, causes a permission denied error, as is common for cross domain scripting. You can accomplish this task relatively easily with server-side (I know how easily with PHP) though, so if you're interested...
0
 
LVL 3

Author Comment

by:Mike R.
Comment Utility
Um...never mind on the last post.  For whatever reason, if I tested the page from my local hard drive, it choked...but if I actually published it to the web and tried it...it worked.

Go figure...Microsoft...:-)
M
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Heh, good for you :-)
0
 
LVL 3

Author Comment

by:Mike R.
Comment Utility
By the way...I did some surfing, and played with the code a little (to make it http with Javascript, instead of .xml) and this is what I came up with.  I could not have done it if it weren't for your guidance!!

Thanks again and I think we can oficially call this one "solved!"
M

****************
<html>
<head>
<script type="text/javascript">
function getPage(page)
{
var objHTTP = new ActiveXObject("Microsoft.XMLHTTP")
var testPage='http://www.city-data.com/zips/'+page+'.html'
objHTTP.Open('GET',testPage,false)
objHTTP.Send()
var test= objHTTP.status
 if (test==200){
  return true;
  } else {
  return false;
 }
}
</script>
</head>

<body>

<form onsubmit="if (getPage(this.myURL.value)) {this.action='http://www.city-data.com/zips/'+this.myURL.value+'.html';} else {this.action='http://www.city-data.com/';}" method="POST">
                  <p align="center"><input type="text" name="myURL" size="5"><input type="submit" value="Get City Info!"><br>
                  <b>Provided by <a href="http://www.city-data.com">CITY-DATA.COM</a></b><br>
                  <b>If the page comes up as unavailable, the ZIP code may not yet be
                  in City-Data's database.<br>
                  Enter a nearby ZIP, and try again!</b></p>
            </form>


</body>
</html>

*************
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
No problem :). Only point I have to make was that in my original code, it should work for Mozilla Firefox and Netscape for local documents at least. That is why the xmlHttpRequest is there, because NN needs it, but in the end, it's your choice, since Mozilla doesn't seem to let you do cross-browser.
0
 

Expert Comment

by:dickarscott
Comment Utility
I tried this also but my server, although forming the URL correctly is not giving forth the PDF document I am trying to present...

Here's my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PDF Roll Call Retrieval</title>
</head>
<body bgcolor="#ffffff">
<p>Retrieve Roll Call PDF
<form name="GetRollCallVote" action="" method="post">
All Roll Call Votes are in PDF&nbsp;format. Please put in the number you are looking for<br>
and add the neccessary leading zeroes to make a five digit number
<p><input type="text" name="RollCallURL" size="5">
<input type="submit" value="Get Roll Call" onclick="this.form.action='RollCallPdfs/'+this.form.RollCallURL.value+'.PDF';" >  <input type="reset" value="Clear Form Data" ></p>
            </form>
      </body>
</html>

I suspect it's may be a server thing but I reset all the necessary permissions. Do I have to add a MIME type somewhere in the code for the PDF?

Thanks
Dick
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
dickarscott, not too sure how your problem pertains to this. Can you elaborate further?
0
 

Expert Comment

by:dickarscott
Comment Utility
The question and answer is on target and formed the proper URL but it appears the server needs some config to feed up the PDF document. It appears that the server error 405 received is related to application mapping in IIS 5. The PDF called in the URL is not loading into the browser window even though mime type in browser is there.

Now it looks as though the code is correct but the PDF application mapping for that extension isn't right. When I map .pdf to AcroRd32.exe I get an invalid filepath error. I guess there must be some pdf.dll that needs to be in the right place to get the path fixed.

That's really my need.
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now