How to get variable from javascript env. and use for PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ruzanna
Forum Newbie
Posts: 1
Joined: Thu Oct 07, 2004 2:21 am

How to get variable from javascript env. and use for PHP?

Post by Ruzanna »

Hello everyone,

Please help me, I begginer and can't solve one question.

How can I get variable from javascript environment and use for php variable?

Beforehand thankfull,
Ruzanna
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

PHP could only access the variable if you refreshed the page and passed it via POST (e.g. a form), via GET (ie. in the URL) or in a cookie.

Mac
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

hidden

Post by phpScott »

you would have to use hidden fields in your html form and use javascript to set that field then the value would be passed when the form gets submited.
then it would be like twig said
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

Or, is this what you meant:

Code: Select all

<?php
$variable = "<script language"javascript">document.write(JavaScript_variable);</script>";
...

?>
NOTE - JavaScript_variable first needs to be declared inside the JavaScript so you could "write it" inside the php.

like this:

Code: Select all

<script language"javascript"> 
JavaScript_variable = document.form_name.field_name.value
</script>
fill the
form_name = with the name of your form
and
field_name = with the field you want to pass


NOTE - this is the code only if the JavaScript and PHP are on the same page.


Hope this helps.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

twigletmac wrote:PHP could only access the variable if you refreshed the page and passed it via POST (e.g. a form), via GET (ie. in the URL) or in a cookie.

Mac
Basically, yes - but the lines are blurring:

http://www.sitepoint.com/blog-post-view.php?id=191776
http://www.sitepoint.com/blog-post-view.php?id=192138
http://www.sitepoint.com/blog-post-view.php?id=193677
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

like I still don't have enough to figure out with XML now you have to go and throw this into the works. Thanks patrickG.
I been using the IFRAME method for a while and it does work providing you are willing to do lots of coding in JS. It is fairly straigth forward but referencing around the different windows can get tiresome.
Post Reply