AJAX / LocalStorage: Is it possible to pass a JS variable in PHP?

I was wondering what would be the best way to store a basket?
I thought about storing the product id in localstorage / sessionstorage and then used AJAX to retrieve the products from the server.
The only problem is that I don't know how to pass elements from localstorage to PHP ...
I suppose this is probably not possible directly, and I thought about the form, although I don’t know how to implement it ...
The idea is that when the user clicks "Add to Cart", the identifier and the number of elements are added to the local resource, and when the user views his cart, the product details (image, name, price ...) are retrieved from the database. I will probably use this tutorial
http://verido.dk/index.php/ressourcer/artikler/loading-and-saving-data-dynamically-using-php-jquery-and-mysql/

I could also give up local storage and go with sessions and a database, but I would really like to give users this dynamic web interface.

Another possible way I just came up with is to use urls like file.php? prodid = ......, although URLs like this may be too long.

Thanks in advance!

+4
source share
4 answers

'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!

+4
source

Use AJAX to send data to the server (GET or POST) and store these values ​​in a session variable or in a cookie on the server

+2
source

You can use local / session storage data and fill in some hidden inputs and then submit the appropriate form to the ajax request. To make things cleaner, you can use one hidden input and set its value to a JSON object created from your storage data.

+1
source

No, javascript runs on the client, php runs on the server.

-3
source

All Articles