PDA

Click to See Complete Forum and Search --> : Passing a variable into a script through the URL


JJR512
Dec 11th, 2000, 02:00 PM
I have a cgi script that basically writes an entire web page. Actually, it writes the common parts of the webpage, so it's basically a template. In the middle of it, where the actual content goes, there is a line that pulls in the content from a separate file. It is a require statement, and requires a .pl file. So, for example, I would have a reviews page, called review.cgi, with a require line like require "reviews.pl";, or a links page like links.cgi with a require line like require "link.pl";.

The thing is, those cgi files, like reviews.cgi and links.cgi, are all exactly identical, except for the require line. I'm thinking there must be a better way to do it.

I think that there is a way to use a URL to pass a variable into as script. I think it is something like http://www.websiteurl.com/file.cgi?variablename=variablevalue. But I don't know exactly how it works.

So, how do I do that? I would probably call the variable 'file', and the cgi file would be named main.cgi. What do I have to do to the cgi file so that it gets the variable from the URL that somebody used to open the file, and get it to work in the require line?

Thanks in advance for any help!

RealisticGraphics
Dec 12th, 2000, 06:42 PM
It's easy. Here is an example (and yes I know 5 is not greater than 5, but it made it shorter).


<script language="JavaScript">
//Set a Var = the returned value from the location string
//Slice(9) means that you are cutting off 2 characters for
//the ? and the = symbols. The other 7 are to cut off
//PassNum which is the name of the textbox in the form
//This is done because we don't want that information in
//this example:

var Num = window.location.search.slice(9)

//From here on it is simple javascript and html.
if(Num < 5){
alert("Num is less than " + Num)
}else if(Num >= 5){
alert("Num is greater than " + Num)
}

</script>

<form>
<input type="text" name="PassNum"><br>
<input type="submit">
</form>


Let me know if you need anything else. I've found that this search function is really useful in filtering out cgi scripts and going to js instead. BTW - In IE this script may not work if you run it locally on a machine, for some reason IE tends to only let it work on remote files (web servers).

Dec 14th, 2000, 10:43 AM
When the "GET method" is used to send data to a cgi script (typically from an html FORM), the cgi script will see the data in the cgi environment variable "QUERY_STRING".

Typing in this in a browser (where you type the url's):
"http://www.websiteurl.com/file.cgi?variablename=variablevalue" will set the cgi environment variable "QUERY_STRING" in this instance of file.cgi equal to "variablename=variablevalue".

If you are getting the data from an html page, the "form tag" in the html file could look like this:

<html>
<head>
<title>HTML Input Form</title>
</head>
<body>
<center>
<form action="http://www.websiteurl.com/file.cgi" method="GET">
<input type="text" name="variablename">
<input type="submit" value="Text You Want On The Button">
</form>
</center>
</body>
</html>


RealisticGraphics actually is showing a way to have JavaScript process data within a form without calling a cgi script back on the server side. That is an interesting approach if you don't mind exposing your code and you don't want to bog down your server (instead, you let the client's machine do the processing). You may need to send data back to the server though. As Is, I didn't get the JavaScript code to work, but I see the concept.

RealisticGraphics
Dec 14th, 2000, 12:40 PM
VirtuallyVB:

I'm gonna take that as a compliment :) (Not sure if thats what you intended). I notice that although the script I wrote here is functional, I was wrong in stating that "Num is less than the variable. Although the output in the script was fine.

Also, I noticed you said you couldn't get mine to work. If you use IE it will only work when the script is on a server, if you place it on a local drive and call the path in IE as "C:\test.htm?PassNum=4" IE will simply reload the page and ignore the search string. I've not figured out why just yet however. Netscape will work fine no matter where it is.

Dec 14th, 2000, 01:58 PM
RealisticGraphics
It was a compliment to your alternate method. I don't think JJR512 was asking for that, but I commend "Thinking Outside Of The Box". Also, you taught me something because I never looked at it from that point of view. It may come in handy for me or others reading this thread.

I caught that comment about not working on IE. Yes, I was using IE when it didn't work. Now I confirmed that it works for Netscape.

I am thanking you for the concept of using JavaScript.

RealisticGraphics
Dec 14th, 2000, 02:07 PM
Figured you did. Thanks. I've actually made an extremely functional search engine using this function. I work with a lot of intranet developement and small business pages where there is no access to CGI or other high end programming tools. As high as it gets is JavaScript (sometimes vbs). And as long as you are working locally this script is to slow at all but you are right that when using it on the internet it can extremely slow, but hey, we work with what we got, right?

Dec 14th, 2000, 03:33 PM
I think your technique could help this guy http://forums.vb-world.net/showthread.php?threadid=43064 . He may be looking in the wrong place for his solution. Perhaps he should use JavaScript instead of Java.