Rectangle 27 0

Data transfer from JavaScript to PHP?


<script language="javascript">
   function load(){
      document.getElementById('myAnchor').href="test2.php?sw="+screen.width+"&sh="+screen.height;
   }
</script>
<body onload="load();">
<a id="myAnchor" href="#">Send to server</a>
<?php
if (isset($_GET)){
    print_r($_GET);
}
?> 
</body>

If you don't know AJAX then you can use links to send information to the server with the GET method.

Or you can also use forms to send information to the server.

Note
Rectangle 27 0

Data transfer from JavaScript to PHP?


Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

@Trava check out jQuery's Ajax examples: api.jquery.com/jQuery.ajax it's easy to get into, just a few lines of code.

@Trava no, it can't be done without Ajax, sorry. Javascript runs after PHP has generated the page, and has no chance of passing anything back to PHP. This is why Ajax exists.

I'm so bad in AJAX. It could be done just with javascript and php. When they are on the same page.

You would have to make an AJAX call from JavaScript to a PHP script. That PHP script would save the screen resolution in the current session (you'll need to use sessions for this). A PHP script requested at a later point could then access the screen resolution as passed by the JavaScript snippet.

Note