<script> var widthBlock = $('#block').width(); document.write(widthBlock); </script> 

As the value that is output by document.write(widthBlock); put a variable in PHP?

    2 answers 2

    The variable can be passed to PHP via a cookie or sent by an ajax request. For me, the second is better. It will look something like this.

     <script> var action = 'your_script.php'; var widthBlock = $('#block').width(); $.post(action, { var_name: widthBlock }, function()); </script> 

    Another such moment. To determine from which user the information came in the php script, you can use the PHP session ID. It will simultaneously be stored and match in the PHP script and in the browser cookie.

    Read more about sessions in PHP: http://www.php.su/articles/?cat=examples&page=070

    Library for working with cookies from JS: https://github.com/js-cookie/js-cookie

    • And here is the session and your example?) - Urmuz Tagizade
    • If the task is to simply transfer the width of the block to a variable in a php script (for example, to collect statistics on the page display from users), then you can not use the session. If you need to do a specific action for a specific user with a specific block width, you need to use any unique identifier for this user. I suggested a session identifier (if for example the site does not have an authorization system or any other mechanism for uniquely determining a user). - Mitrohin
    • I understand why we need sessions and everything else. It is advisable to describe your sayings in response. You can use a lot of things. - Urmuz Tagizade

    This solution is ideal for your case:

     var widthBlock = $('#block').width(); window.location.href = "index.php?widthBlock=" + widthBlock; 

    To get the widthBlock value, you need to refer to the $ _GET ['widthBlock'] variable in the index.php file