Link to home
Start Free TrialLog in
Avatar of Mike R.
Mike R.

asked on

Create a URL link based on input text

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
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike R.
Mike R.

ASKER

Tha did it...thanks! (I new it would be simple!  This is what happens when you have Net Admins doing web pages :-)
Heh, you need any more help, just post here. I know a lot of great EE people that're bound to help you
Avatar of Mike R.

ASKER

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
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;}">
Avatar of Mike R.

ASKER

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
Nope, nope, don't save it as HTML. Put the function in your <script> tag on your page :)
Avatar of Mike R.

ASKER

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
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...
Avatar of Mike R.

ASKER

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
Heh, good for you :-)
Avatar of Mike R.

ASKER

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>

*************
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.
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
dickarscott, not too sure how your problem pertains to this. Can you elaborate further?
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.