Jump to content

Archived

This topic is now archived and is closed to further replies.

kyleabaker

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

Recommended Posts

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...
[code]($ clicks) <a href='$ url' onClick='increment($ number)'>$ name</a>[/code]
...so it looks like (45) Link1 or something like that. My real question is how do I do something like this...
[code]
< script>
function increment (n) {
<- ? php mysql _ query(' UPDATE  links   WHERE  number = n $ clicks = $ clicks + 1 ') ; ? >
}
< / script>
[/code]
...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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.