Jump to content


Photo

Using php in a javascript function, how to use a js varible in php section


  • Please log in to reply
2 replies to this topic

#1 kyleabaker

kyleabaker
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 07 October 2006 - 01:19 AM

Hey guys,
I made a links page on my website recently and I wanted it to increment a number in my MySQL database for the individual link entry in the database each time it is clicked. I have the page php setup to print out each row in the links table from my database in the format of...
($ clicks) <a href='$ url' onClick='increment($ number)'>$ name</a>
...so it looks like (45) Link1 or something like that. My real question is how do I do something like this...
 < script>
 function increment (n) {
 	<- ? php mysql _ query(' UPDATE  links   WHERE  number = n $ clicks = $ clicks + 1 ') ; ? >
 }
 < / script>
...where n is the varible used in the javascript function. I'm guessing they don't follow through and aren't usable in the php code? if not how could I make use of them? or is there a better way to do this?

I had to mess up to format of that code above before it would post this message, but maybe you can still understand my question. Also, I wasn't sure if php could be used in the middle of a javascript function in this way, but if not maye there is another better way to do what I'm trying to do.
Thanks in advanced,
kyleabaker

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 October 2006 - 01:33 AM

Javascript happens on the client, while php on the server. The only way you can get javascript variables into php is to submit them back to the server. Many ways to do this, most need a page refresh, or you could try using some ajax.

#3 kyleabaker

kyleabaker
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 07 October 2006 - 06:12 AM

Oh I see. Before I go test this, what would happen if I just added the correct php script to the onClick event of each url plugging in the proper number so they are all set correctly? Would the onClick event actually run the php script? or would there be anyway to make the onClick even run the code if I can insert the exact script in each url's onClick event? Thanks in advanced!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users