Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1785
  • Last Modified:

How do I use client side localStorage variables in server side php?

I am capturing several data elements in localStorage variables. I need to retreive these variables in my php code to pass them into mysql db. How do I pass or retreive the localStorage variables from the client-side to be accessible on the server-side?

Code snipet:

<?php
   $conn = mysql_connect("localhost", "X", "Y") or die(mysql_error());
   mysql_select_db("test");
                  
   $vardate = date("Y-m-d");
   $vartime = date("H:i:s");
   $username = localStorage.txtUser;                                  
                              
   mysql_query("INSERT INTO itemhdr (ukey, user, trxdate, trxtime, email_add, email_sent)
   VALUES ('1234567890', '$username', '$vardate', '$vartime', 'xyz@kbios.com', 'N')");
                  
   mysql_close($conn);
?>
0
kbios
Asked:
kbios
  • 5
  • 4
  • 3
  • +2
2 Solutions
 
russellCCommented:
You can pass them as form variables and use
$_GET["NAME"] or $_POST["NAME"]
0
 
kbiosAuthor Commented:
Here is the code now. After the INSERT the mysql field is blank. I KNOW that localStorage.txtuser contains text data. I'm familiar with $GET when retreiving info from an URL. No URL data is being passed when the php is getting called.


<?php
   $conn = mysql_connect("localhost", "X", "Y") or die(mysql_error());
   mysql_select_db("test");
                 
   $vardate = date("Y-m-d");
   $vartime = date("H:i:s");
   $username = $_GET["localStorage.txtuser"];                                  
                             
   mysql_query("INSERT INTO itemhdr (ukey, user, trxdate, trxtime, email_add, email_sent)
   VALUES ('1234567890', '$username', '$vardate', '$vartime', 'xyz@kbios.com', 'N')");
                 
   mysql_close($conn);
?>
0
 
Dave BaldwinFixer of ProblemsCommented:
"localStorage" is not a PHP function but a DOM function accessible thru javascript in the browser (not on the server).  http://www.webmonkey.com/2011/04/how-to-use-html5s-local-storage-tools-today/  You will have to use javascript and probably AJAX to pass the info to your PHP page as name/value pairs like "http://www.yoursite.com/mydata.php?txtuser=This%20is%20text".
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kbiosAuthor Commented:
I understand that localStorage is client-side not server-side. I also am familiar with passing the name/value pairs within the URL and using $GET. That works with a small amount of data that is needing passed. In my case I'll have numerous name/value pairs that will need to get to php to be able to be loaded into a mysql db.

Any thoughts on how to handle many name/value pairs? Think of it as a small db being stored in localStorage and then needing to get INSERTED into mysql. Any other thoughts as to how this can be accomplished?
0
 
Ray PaseurCommented:
Can you please show us your test case with numerous name/value pairs in the localStorage?  Thanks.
0
 
themrrobertCommented:
I understand precisely what you are trying to do, but I can see the knowledge you are missing.

Go here:

http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml

Use the localStorage.whatever variables in the JAVASCRIPT that you use to build the url.

When building the url in ajax,  look to the attached code (customize properly for your code)

Then in php, call the code like this:

$name =  $_GET['name'];
$age = $_GET['age'];

and this is how you access the variables in php.


var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
 if (mygetrequest.readyState==4){
  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
   document.getElementById("result").innerHTML=mygetrequest.responseText
  }
  else{
   alert("An error has occured making the request")
  }
 }
}
var namevalue=encodeURIComponent(document.getElementById("name").value)
var agevalue=encodeURIComponent(document.getElementById("age").value)
mygetrequest.open("GET", "basicform.php?name="+localStorage.namevalue+"&age="+localStorage.agevalue, true)
mygetrequest.send(null)

Open in new window

0
 
themrrobertCommented:
the code at the bottom is java script not php,

the way the attachment layout is makes it difficult to organize.
0
 
kbiosAuthor Commented:
localStorage.txtuser = "userx"
localStorage.item1 = "itema"
localStorage.item2 = "itemb"
localStorage.item3 = "itemc"
localStorage.item4 = "itemd"
localStorage.item5 = "iteme"
.........   etc  

There could be a 1000 items in this localStorage 'db'. Think of it as a client-side shopping cart that has been created. Each of the localStorage items will need to be INSERTED INTO a mysql db. I realize that I could pass each item to the .php and singularly update the db. But I don't want to have to call the .php for each item. I'm hoping to somehow create a loop to retreive each of the localStorage items and create the INSERT INTO record.
0
 
themrrobertCommented:
Yes Ray will be able to solve any problem :) I didn't see your post ;) If you need further help i'm sure he has got you handled
0
 
kbiosAuthor Commented:
themrrobert - I'm looking at the AJAX info. Quick question. If I end up building an equivalent URL that uses $_GET why not just build a URL passing the localStorage variables to the .php? I'm trying to see the difference/benefit of using AJAX vs. passing the variables in the URL.
0
 
Dave BaldwinFixer of ProblemsCommented:
With 1000 items, you will need to make it a POST instead of a GET.  GET will be limited to about 2000 characters in Internet Explorer.  Your example would be at least 12000 characters in name/value format.  To create the POST data, you will have to loop thru your storage and append each value to the POST string.  In the PHP page, you will have to have code that will go thru that data and put it where you need it.
0
 
Dave BaldwinFixer of ProblemsCommented:
AJAX can use GET or POST.  Those are the HTTP methods available for making requests to a server.
0
 
Dave BaldwinFixer of ProblemsCommented:
The link that @themrrobert posted is my favorite resource for AJAX info.
0
 
kbiosAuthor Commented:
Thanks for the help. I need to process and test the the info you provided.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now