'You must remember that Server-Side (PHP) code is read before it converts the code into browser-side code. JavaScript is controlled by the browser ...
So one-dimensionally, you cannot pass JavaScript to PHP.
However...
With Ajax and in your case, I suggest JSON, you can send JavaScript data to a PHP page and return the response to your JavaScript methods. I think this will suit your needs. I can provide a simple example if you want.
// - example below:
JavaScript:
//Ajax Method function processAjax(queryString, processDiv, responseDiv) { responseDiv.innerHTML = ''; var myAjax; try { // Modern Browsers--> myAjax =new XMLHttpRequest(); } catch (e) { // antique ie browsers--> try { myAjax =new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ myAjax =new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { // Something went wrong document.getElementById('processDiv').innerHTML=""; alert("Your browser malfunctioned! Please try again. Consider installing a modern browser if the problem persists."); return false; } } } myAjax.onreadystatechange = function() { if (myAjax.readyState == 4) { var ajaxResponse = myAjax.responseText; responseDiv.innerHTML = ajaxResponse; processDiv.innerHTML = ""; //NOTE: HERE IS WHERE AJAX IS FINISHED, SO IF YOU WANT TO DO SOMETHING ELSE HERE YOU CAN! //POST PROCESSING-----> // IE: alert('I am finished processing now!'); // or call another function: // anotherFunction(); } else { processDiv.innerHTML = '<img src="http://www.mysite.com/images/loadingicon.gif" alt="processing....." />'; } } myAjax.open("GET", queryString, true); myAjax.send(null); } function sendStorage() { var helloVar = 'Hello, I am passed to PHP #1'; var worldVar = 'I am the second value passed to PHP!'; var processId = 'process_div'; var resultId = 'result_div'; var queryString = 'http://www.mysite.com/process.php?hello=' + helloVar + '&world=' + worldVar; processAjax(queryString, processId, resultId); }
Now for some HTML:
<div id="content"> <div id="process_div"> This is where processing will occur </div> <div id="result_div"> This is where my response will display </div> </div>
Now for process.php (NOTE: FOR SAFETY, I STRONGLY SUGGEST THAT YOU DO NOT RESPOND TO THE SERVER PAGE IN YOUR JAVASCRIPT)
<?php //init $hello = ''; $world = ''; $errors = 0; //set //Security note: never trust a URL request.. you should clean all $_GET, $_REQUEST, AND $_POST with the PHP htmlspecialchars() method (take a look at php.net for that) (isset($_GET['hello'])) ? $hello = $_GET['hello'] : $errors++; (isset($_GET['world'])) ? $world = $_GET['world'] : $errors++; //process if($errors > 0) { echo 'Errors Detected! Missing querystring get data!'; } else { echo '<p>Hello received: ' . $hello . '</p>'; echo '<p>World received: ' . $world . '</p>'; //now we can process $hello and $world server side! }
? >
Important: you really need to learn some JSON and $ _POST requests, as they are more secure, faster, and you can easily manipulate the returned data. I suggest looking at the library as jquery for simplified examples.
I have not tested this code, but it should work. Let me know if you have questions or this does not answer your question.
I am glad to help!