473,795 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieve CGI variables from Javascript

Here's the situation. I have a static html page which we want to update
to include some dynamic content. I want a counter that keeps track of
the number of times anyone presses the "add" button, and display that
number. So, that page would look something like:

Number of calls today: 5
Add | Reset

The "5" would increment with every click of the "Add" link. The "Reset"
link would reset the counter to 0.

I have a Perl script that does all of the accounting stuff (opens a file
that contains the number, increments it, resets it, etc). What I don't
know how to do is to get the data from the CGI script to the web page.
I'm imagining that you can use Javascript, but I can't figure it out.
My CGI script can accept three options (add, view, reset). So it you
call it like so [myscript.cgi?ac tion=add], it increments the counter by one.

So, in a nutshell, this is what I want:
1) the web page to display the # of calls upon load.
2) When a user presses the "Add" link, it invokes the CGI script to
handle the accounting stuff, then refresh the page with the new number
of calls.
3) When a user presses the "Reset" link, it resets the counter to 0.

Jul 20 '05 #1
6 13823
Geoff wrote on 03 Dec 2003:
Here's the situation. I have a static html page which we want
to update to include some dynamic content. I want a counter
that keeps track of the number of times anyone presses the "add"
button, and display that number. So, that page would look
something like:

Number of calls today: 5
Add | Reset

The "5" would increment with every click of the "Add" link. The
"Reset" link would reset the counter to 0.

I have a Perl script that does all of the accounting stuff
(opens a file that contains the number, increments it, resets
it, etc). What I don't know how to do is to get the data from
the CGI script to the web page. I'm imagining that you can use
Javascript, but I can't figure it out. My CGI script can accept
three options (add, view, reset). So it you call it like so
[myscript.cgi?ac tion=add], it increments the counter by one.

So, in a nutshell, this is what I want:
1) the web page to display the # of calls upon load.
2) When a user presses the "Add" link, it invokes the CGI script
to handle the accounting stuff, then refresh the page with the
new number of calls.
3) When a user presses the "Reset" link, it resets the counter
to 0.


If you want to insert the value directly into the document, try:

<SCRIPT type="text/javascript">
document.write( <?php echo "\'" . $counter . "\'"; ?> );
</SCRIPT>

....which after evaluation (if counter was 10) should be:

document.write( '10' );

I don't know Perl, so the I've shown a (hopefully) comparible PHP
example. Basically, you need to include the counter value as a quoted
string inside the document.write call.

Alternatively, if you want to assign the value to a JavaScript
variable, you'd do something like (again, in PHP):

<SCRIPT type="text/javascript">
var counter = <?php echo $counter; ?>;
</SCRIPT>

....which after evaluation (if counter was 10) should be:

var counter = 10;

JavaScript can't access a CGI (or PHP) variable - they are used
server-side, and JS is client-side. However, you can give it the
value stored in the variable at the time of parsing by outputing the
variable's value directly into the document.

For the control buttons:

<SCRIPT type="text/javascript">
function invokeAction( type ) {
window.location = 'myscript.cgi?a ction=' + type;
}
</SCRIPT>

<BUTTON type="button" onclick="invoke Action('add')"> Add</BUTTON>
<BUTTON type="button" onclick="invoke Action('reset') ">Reset</BUTTON>

Clicking those buttons would change the page to:

myscript.cgi?ac tion=add

and

myscript.cgi?ac tion=reset

respectively.

<snipped HTML attachment>

This is a text-only newsgroup. Please only post in plain text: no
HTML and no attachments.

Hope that helps,
Mike

--
Michael Winter
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #2
Michael Winter wrote:
If you want to insert the value directly into the document, try:

<SCRIPT type="text/javascript">
document.write( <?php echo "\'" . $counter . "\'"; ?> );
</SCRIPT>

<SCRIPT type="text/javascript">
var counter = <?php echo $counter; ?>;
</SCRIPT>

For the control buttons:

<SCRIPT type="text/javascript">
function invokeAction( type ) {
window.location = 'myscript.cgi?a ction=' + type;
}
</SCRIPT>

<BUTTON type="button" onclick="invoke Action('add')"> Add</BUTTON>
<BUTTON type="button" onclick="invoke Action('reset') ">Reset</BUTTON>

Clicking those buttons would change the page to:

myscript.cgi?ac tion=add

and

myscript.cgi?ac tion=reset

respectively .

Hope that helps,
Mike

Unfortunately, I don't know PHP, so I don't really understand your
expample. I tried using a Server Side Include, but that failed..
Here's a snippet of code:
"<!--#exec cgi="/cgi-bin/callTrack.cgi action=view"-->"

In my mind, this calls callTrack.cgi and passes the parameter
action=view. This doesn't work.

As far as the buttons go, I don't want the output of
callTrack.cgi?a ction=add to be printed to the screen, I just want it to
update the file that contains the current call count. So, that's why I
wanted to use javascript so I could do something like:
execute callTrack.cgi action=add
reload this page.

I hope I'm making myself clear.

thanks,
Geoff

Jul 20 '05 #3
Geoff wrote on 03 Dec 2003:

<snipped my post>
Unfortunately, I don't know PHP, so I don't really understand
your expample. I tried using a Server Side Include, but that
failed.. Here's a snippet of code:
"<!--#exec cgi="/cgi-bin/callTrack.cgi action=view"-->"
The example didn't really require knowledge of PHP. The idea was to
show how to assign a server-side variable to a JS variable. I just
used PHP syntax, because that's the only syntax I know. You could
replace the PHP code with CGI equivalent for:

var jsVar = writeCGIVariabl e( $counter );

Where writeCGIVariabl e is a CGI function that takes a CGI variable
($counter) and outputs it to a text string with a trailing semicolon
so that when the browser gets it, it looks like:

var jsVar = <some number>;

That was all I was trying to show.
Note: The document.write example is pointless. If you can output
text, why output text for JS to then output again?
In my mind, this calls callTrack.cgi and passes the parameter
action=view. This doesn't work.

As far as the buttons go, I don't want the output of
callTrack.cgi?a ction=add to be printed to the screen, I just
want it to update the file that contains the current call count.
So, that's why I wanted to use javascript so I could do
something like:
execute callTrack.cgi action=add
reload this page.

I hope I'm making myself clear.


OK. In order to update the value on the server, it's obvious that you
need to make another HTTP request, during which time the count is
updated. There are two possible (related) ways:

1) Do what my buttons did: navigate to another page. The page can
have exactly the same contents as it did before, but during the
reload, the CGI script is invoked and the count is updated.
2) Hide something somewhere that gets reloaded (instead of the page)
and updates the counter.

The first option is quite a bad idea as the user has to re-request
the entire page, and gets nothing from it (no new content). The
second is a fudge, but is transparent to the user.

The hidden object can be anything, really. What would be ideal is if
your update script (callTrack) always returned a small transparent
GIF, rather than HTML. I don't know if you can do it, though. With
this approach, you would include an image, and update the source when
the buttons were clicked:

<IMG id="dummy" src="callTrack. cgi">

[Doesn't alter the script's values - just gets the transparent GIF]

<SCRIPT type="text/javascript">
function updateCount( action ) {
document.getEle mentById('dummy ').src = 'callTrack.cgi? action='
+ action;
}
</SCRIPT>

<BUTTON type="button" onclick="update Count('add')">A dd</BUTTON>

As I said, you could theoretically update any object. For example, an
IFRAME (it would use the same script and BUTTON above):

<IFRAME id="dummy" src="callTrack. cgi" width="1" height="1"
style="display: none">

You wouldn't have to return binary data here. You could return
nothing at all.

To be honest, this is far from my area of expertise, so there may be
more elegant ways of doing it, but they would still boil down to
sending a HTTP request of some description.

My final note: if the user has JavaScript disabled, this won't work.
You could replace the buttons with regular links if that's a concern,
but then you'd be limited to only the first option I presented above
(a full page refresh).

Mike

--
Michael Winter
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4
Server side includes such as this one are disabled by default
and not supported by all web servers.

This will work with apache if this service is enabled.
Unfortunately, I don't know PHP, so I don't really understand your
expample. I tried using a Server Side Include, but that failed..
Here's a snippet of code:
"<!--#exec cgi="/cgi-bin/callTrack.cgi action=view"-->"

Jul 20 '05 #5
callenstrc
2 New Member
The following link shows you how to get the CGI variables in Javascript

http://javascript.geniusbug.com/inde...&name=passVars
Jun 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4130
by: Eugfene | last post by:
I have the following function in a html file: function selectEdit(fileID, processCode, processID, fileName,fileDesc) { document.forms.recordID.value = fileID; document.forms.processor.value = processCode; ` document.forms.processorID.value = processID; document.forms.interfaceFileName.value = fileName; document.forms.fileNameDesc.value=fileDesc; document.forms.submit();
9
3726
by: Colin Graham | last post by:
hi folks, just wonder if this is possible or is there a better way of doing this. i need to store values in cookies between two forms. the details are added in a popup when this is closed the main form stores these values in hidden fields. at this point xml is built up and submitted to a biztalk server. if there is an error the page is refreshed and the initial values i added via the popup are lost. my idea was to store these as...
9
6832
by: Don | last post by:
Say I have a class like so: Public Class MyClass Public Prop1 as Integer Public Prop2 As Integer Public Prop3 As Integer End Class Is it possible to retrieve a list of the variables or objects declared within an instance of that class if they are declared with Public (or
5
2443
by: ggk517 | last post by:
We are trying to develop an Engineering application using PHP, Javascript with Informix as the back-end. Is it possible to retrieve data using Javascript but by accessing the Database. Say somebody enters part_no, than using Javascript is it possible to connect to the part master and retrieve the division and desc information? I am not allowed to use the PHP because this will require the user to insert the part number on the first...
11
1935
by: Patricio | last post by:
Hello all I open a PopUp that receives two variables (values). This PopUp must return a value as well. How can I retrieve this value? Thanks a lot.
6
4357
by: Daz | last post by:
Hi everyone. Firstly, I apologise if this i not what you would call a PHP problem. I get quite confused as to what lives in which realm, so if this shouldn't be posted here, please suggest where I should post it. I have created a form, which consists of a list of items, each with a checkbox. When a checkbox is checked or unchecked, the page should be refreshed. During the refresh, the data is validated and the MySQL database is...
5
25587
by: Ken | last post by:
I'm trying to run a loop to capture column property information from a table in my datasource. Can anybody see where this is going wrong? Dim tbl As New DataTable Dim col As DataColumn Dim x As Integer Dim colName(99) As String Dim colType(99) As String cn.Open() tbl = cn.GetSchema("Orders") 'Orders is a table in the
1
2184
by: Tarik Monem | last post by:
Here's the code for the index.xml file on the blogger <?xml version="1.0" encoding="<$MTPublishCharset$>"?> <rss version="2.0"> <channel> <title><$MTBlogName remove_html="1" encode_xml="1"$></title> <link><$MTBlogURL$></link> <description><$MTBlogDescription remove_html="1" encode_xml="1"$></description>
3
2655
by: David | last post by:
Hi, I have some code in my form as follows, to display 1 to 20 additional sets of fields to enter guest information. I am not sure how to retrieve these guests info so that I can post the info on an email such as: Additional Guest 1 First Name = Last Name =
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.