473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?action=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 13791
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?action=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?action=' + type;
}
</SCRIPT>

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

Clicking those buttons would change the page to:

myscript.cgi?action=add

and

myscript.cgi?action=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.******@blueyonder.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?action=' + type;
}
</SCRIPT>

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

Clicking those buttons would change the page to:

myscript.cgi?action=add

and

myscript.cgi?action=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?action=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 = writeCGIVariable( $counter );

Where writeCGIVariable 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?action=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.getElementById('dummy').src = 'callTrack.cgi?action='
+ action;
}
</SCRIPT>

<BUTTON type="button" onclick="updateCount('add')">Add</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.******@blueyonder.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
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
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 =...
9
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...
9
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...
5
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...
11
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
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...
5
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...
1
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"...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.