PHP & Javascript

Started by
2 comments, last by Kasya 13 years, 3 months ago
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 &#106avascript file

and in myfile.php
i put the header of &#106avascript file and<br>I have in myfile.php form for name and age when I make run myfile.php <br>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.<br><br><br>the second &#111;ne used<br><br>document.getElementById('countV').value=count <br>in &#106avascript<br><br>and in myfile.php I used form<br>and the value of php variable is empty!!!<br><br><br>please I don't have time<br><br>I want answer<br><br>ThankYou<br>
Advertisement
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.
You cannot assign a &#106avascript variable to PHP, simply because the PHP is completely executed and served by the time the &#106avascript runs.<br><br>You CAN pass variables from PHP to the &#106avascript however. Just embed some data in the page somewhere:<br><br>&lt;input type="hidden" name="foo" value="bar" /&gt;<br><br>You CAN pass data back to the server from the running &#106avascript by using AJAX or your favorite &#106avascript 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.<br><br>You CAN pass information back to the server by writing data to a cookie via &#106avascript, and then reading the cookie in PHP &#111;n the next page load.
I don't what you are trying to do but this should work:

//myfile.phpif(!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/>';}

This topic is closed to new replies.

Advertisement