Jump to content


Photo

pass Javascript data to PHP $variable


  • Please log in to reply
4 replies to this topic

#1 jckrueger

jckrueger
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 20 May 2006 - 11:52 PM

[img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] Greetings!
I need to assign the value from a javascript var to a PHP $variable.

I have been banging my head for days trying to accomplish this task. There has got to be an easy way. Can anyone help me???
jckrueger



#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 21 May 2006 - 12:10 AM

The short answer is that you can't. php only exists on the server and generates a file rendered by your client-side browser. javascript only exists on the client side. So the javascript variable will only exist after the php script has executed.

If you explain a little more, there's probably ways to accomplish what you want to do (just not the way you described).
Legend has it that reading the manual never killed anyone.
My site

#3 flamerail

flamerail
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 21 May 2006 - 01:58 AM

[!--quoteo(post=375597:date=May 20 2006, 08:10 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 20 2006, 08:10 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The short answer is that you can't. php only exists on the server and generates a file rendered by your client-side browser. javascript only exists on the client side. So the javascript variable will only exist after the php script has executed.

If you explain a little more, there's probably ways to accomplish what you want to do (just not the way you described).
[/quote]

I think he wants to assign a javascript code to a var so he can print it certin places.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 02:03 AM

i don't know for 100%, but i think the whole purpose of ajax is to pass things live from client to server and back. but i haven't gotten around to looking into ajax yet, so i couldn't tell you for sure.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 21 May 2006 - 06:52 AM

Yep, that's the whole point of ajax. It uses a predifined javascript class called XMLHttpRequest to exchange variables between a server side script and javascript. In IE the same class is available as build-in ActiveX object. This way you can change PARTS of the page, without refreshing the whole page.

Using Ajax (asynchronous javascript and xml) you can call a php script like this:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]http.open('get', 'phpscript.php?act='+act); [/quote]

As you can see I'm passing javascript var "act" to script "phpscript.php" by way of "get"!

Using:
var response = http.responseText;
I can have all text returned by the serverside script loaded into a variable.

So it works both ways.


There's a bit more to it than that, so go to www.ajaxfreaks.com and check out the beginnertuts. You'll be passing variables before you know it! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users