Pass variable from Javascript to PHP

In my WordPress project I need to pass info from Javascript to PHP code. I’ve tried with all the known methods but was unable to parse the data. How can I do that? These two languages cannot directly share variables. In your case the Javascript is a client-side script and the WordPress PHP code is a server-side technology. You can’t just pass a variable on the fly. You have to generate Javascript code with PHP and let the browser refresh itself! We can pass data to the server code by … posting our data “back” to it.
//  You can insert JavaScript client side code into the page from the PHP:

echo '<SCRIPT language="JavaScript">';
echo 'function MyFunction(){';
echo 'location.href="' . curPageURL() . '?X=1";';
echo '}</SCRIPT>';

// .....
// Wordpress custom function on the  server side
// Current wordpress URL
if ( !function_exists('curPageURL') ) :
    function curPageURL()
    {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }
endif;

// Client side
// Call this JavaScript function from the HTML body
// For example:

<a href="'YOURPATHTODOSOMETHINGELSE" onClick="javascript:MyFunction();"
 target="_blank" class="linkstyle" title="Download our product from here.">Download</a>


//.......
// Back to PHP server side code
//.......

// Get the query and assign the posted data to your PHP variable
// - check and read the posted parameter in your code
// - in this case GET and POST request is handled:

 if (array_key_exists("X", $_REQUEST))
{
      $x= $_REQUEST["X"];
}
This question raised a critical issue for understanding Web applications. We all should understand the difference between client-side and server-side code. A web server can manipulate the code within a Web page before sending it to your browser. For most web pages, no manipulation is done. The server simply sends a copy to the browser and the browser does the work of displaying the code at the client. The programs running on the Web Server are server side programs because they are on the side of the internet that the Web server is on. The browser being used to access the Web site is on the same side of the Web as you, the client side. The WordPress is heavily packed server side code. Good luck with it!