Jump to content
  • Advertisement
Sign in to follow this  
Game P

PHP & Javascript

This topic is 2909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi every one

i want to assign java Script variable to php variable

I have used two methods

The first one used

window.location.href = "myfile.php?counter=" +count;

in javascript file

and in myfile.php
i put the header of javascript file and
I have in myfile.php form for name and age when I make run myfile.php
the page reload many times and I can't stop it and I can't enter the name and age and forced to close the page.

the second one used

in javascript

and in myfile.php I used form
and the value of php variable is empty!!!

please I don't have time

I want answer


Share this post

Link to post
Share on other sites
What are you trying to do? I don't mean about assigning variables, I mean the "big picture". Are you trying to count page accesses? Are you counting the number of times the user tries to do something?

Knowing what you are trying to do will enable us to give you the best advice. And please don't try to hurry us, a forum is not real time.

Unless the count is used on the client, try to store it on the server. Either in the session for temporary data or in the database for persistent data.

Share this post

Link to post
Share on other sites
You cannot assign a javascript variable to PHP, simply because the PHP is completely executed and served by the time the javascript runs.

You CAN pass variables from PHP to the javascript however. Just embed some data in the page somewhere:

<input type="hidden" name="foo" value="bar" />

You CAN pass data back to the server from the running javascript by using AJAX or your favorite javascript library. However, this will not affect the PHP because it has already run. Your server can save this data for later in a database or text file, however.

You CAN pass information back to the server by writing data to a cookie via javascript, and then reading the cookie in PHP on the next page load.

Share this post

Link to post
Share on other sites
I don't what you are trying to do but this should work:


if(!isset($_GET['counter'])) {

echo '<script type="text/javascript">';
echo '//All the stuff to get the count';
echo 'window.location.href = \'myfile.php?counter=\' + count';
echo '</script>';

} else {

echo 'Count is '.$_GET['counter'].'<br/>';


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!