471,596 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

accessing javascript variables within psp code

Hello all,

Question: Is there any way to access a javascript variable from
within psp code?
I'm aware of how to do the reverse of this (js_var='<%=psp_var%>').
Here's a non-working example of what I'm trying to do:
- - - (begin example) - - -
function Test(txt)
{
a = confirm('Are you sure you want to delete '+txt+'?')
if (a)
{
//The problem is getting the value of js variable 'txt' recognized in
"psp space".
<%
os.remove( os.path.join(os.path.dirname(req.filename), '../notes/'+
%>txt<%) )
%>
}
}
- - - (end example) - - -

FYI, I've already found a workaround for the above example (by placing
a "get" variable in the URL, reloading the page, and having psp check
for the existence of that variable before proceeding with deleting the
appropriate file) but I'd still like this general capability for
future projects. I've searched several forums, websites, etc. and
the
answer has aluded me for two days now. I'm new to apache, mod_python,
and javascript, so this may be ridiculously simple.

Since this is my first post on google groups, I'd also appreciate any
helpful suggestions on how to best go about getting answers quickly
(so let me know if there's a more appropriate forum/method/etc.)

Thanks for any help you can provide.

(This is copied from a message I originally posted to the mod_python
group.)

Jul 18 '07 #1
3 2325
On Wed, 18 Jul 2007 04:29:27 +0300, BAnderton <bl************@gmail.com>
wrote:
>
Hello all,

Question: Is there any way to access a javascript variable from
within psp code?
I'm aware of how to do the reverse of this (js_var='<%=psp_var%>')..
Here's a non-working example of what I'm trying to do:
- - - (begin example) - - -
function Test(txt)
{
a = confirm('Are you sure you want to delete '+txt+'?')
if (a)
{
//The problem is getting the value of js variable 'txt' recognized in
"psp space".
<%
os.remove( os.path.join(os.path.dirname(req.filename), '../notes/'+
%>txt<%) )
%>
}
}
- - - (end example) - - -

FYI, I've already found a workaround for the above example (by placing
a "get" variable in the URL, reloading the page, and having psp check
for the existence of that variable before proceeding with deleting the
appropriate file) but I'd still like this general capability for
future projects. I've searched several forums, websites, etc. and
the
answer has aluded me for two days now. I'm new to apache, mod_python,
and javascript, so this may be ridiculously simple.

Since this is my first post on google groups, I'd also appreciate any
helpful suggestions on how to best go about getting answers quickly
(so let me know if there's a more appropriate forum/method/etc.)

Thanks for any help you can provide.

(This is copied from a message I originally posted to the mod_python
group.)
Only with ajax.
Jul 18 '07 #2
BAnderton a écrit :
Hello all,

Question: Is there any way to access a javascript variable from
within psp code?
Short answer : no (or at least: not directly). And it has nothing to do
with PSP.

<OT>
Long answer: this has to do with the http protocol. Things go like this:

1/ the client (usually the browser) send a request (via typing an url in
the address bar, clicking a link, posting a form...)

2/ the server process the request (this is where your psp - or any other
server-side - code is executed) and send back a response (in your case,
what has been generated by the psp code).

3/ the client do something (usually: display) the response. This is
where Javascript - if any, and if supported by the client - is executed.

From the server's POV, once the response is sent, the transaction is
finished.

The only way to have client-side javascript communicate with the server
is via the XmlHttpRequest object (IOW : ajax). This allow javascript
code to send a request to a server without reloading the whole page.
</OT>

(snip)
>
FYI, I've already found a workaround for the above example (by placing
a "get" variable in the URL, reloading the page, and having psp check
for the existence of that variable before proceeding with deleting the
appropriate file)
<OT>
Beware, GET request should *not* have such side-effects on the server. A
GET request is meant - as the name implies - to get data from the
server. Use a POST request instead.

If you're going to do web development, reading the http RFC might be
helpful...
</OT>
but I'd still like this general capability for
future projects.
Then you have to learn how to use XmlHttpRequest.
I've searched several forums, websites, etc. and
the
answer has aluded me for two days now. I'm new to apache, mod_python,
and javascript, so this may be ridiculously simple.

Since this is my first post on google groups,
<OT>
Small correction : you're not posting "on" google groups, you're posting
on usenet *from* google groups. Most people prefer to access usenet via
a newsreader.
</OT>
I'd also appreciate any
helpful suggestions on how to best go about getting answers quickly
http://www.catb.org/~esr/faqs/smart-questions.html

And remember that usenet is definitively not an help desk. No one get
paid for answering your questions.

HTH
Jul 18 '07 #3
On Wed, 2007-07-18 at 01:29 +0000, BAnderton wrote:
Hello all,

Question: Is there any way to access a javascript variable from
within psp code?
I'm aware of how to do the reverse of this (js_var='<%=psp_var%>').
Here's a non-working example of what I'm trying to do:
- - - (begin example) - - -
function Test(txt)
{
a = confirm('Are you sure you want to delete '+txt+'?')
if (a)
{
//The problem is getting the value of js variable 'txt' recognized in
"psp space".
<%
os.remove( os.path.join(os.path.dirname(req.filename), '../notes/'+
%>txt<%) )
%>
}
}
- - - (end example) - - -

FYI, I've already found a workaround for the above example (by placing
a "get" variable in the URL, reloading the page, and having psp check
for the existence of that variable before proceeding with deleting the
appropriate file) but I'd still like this general capability for
future projects. I've searched several forums, websites, etc. and
the
answer has aluded me for two days now. I'm new to apache, mod_python,
and javascript, so this may be ridiculously simple.
>From your vague description it sounds like the contents of your
JavaScript variable come from the server to begin with. In that case
it's easier and more secure to "remember" the correct value on the
server instead of sending it to the client and trusting the client[*] to
send the same value back.

To store variables on the server side between requests by the same
client, use session variables.
[*] A basic premise of web programming is that the client can't be
trusted: Don't trust that maximum form field sizes will be honored,
don't trust that cookies won't be changed, don't trust that GET
parameters won't be changed, don't trust that hidden form fields won't
be changed, etc.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by J. J. Cale | last post: by
12 posts views Thread by Steve Blinkhorn | last post: by
10 posts views Thread by John Passaniti | last post: by
pbmods
1 post views Thread by pbmods | last post: by
4 posts views Thread by MartinRinehart | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.