Solved

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

Posted on 2011-09-22
14
1,610 Views
Last Modified: 2012-08-13
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
Comment
Question by:kbios
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 3

Expert Comment

by:russellC
ID: 36583109
You can pass them as form variables and use
$_GET["NAME"] or $_POST["NAME"]
0
 

Author Comment

by:kbios
ID: 36583183
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 36583229
"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
 

Author Comment

by:kbios
ID: 36583361
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36583403
Can you please show us your test case with numerous name/value pairs in the localStorage?  Thanks.
0
 
LVL 13

Accepted Solution

by:
themrrobert earned 350 total points
ID: 36583449
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
 
LVL 13

Expert Comment

by:themrrobert
ID: 36583456
the code at the bottom is java script not php,

the way the attachment layout is makes it difficult to organize.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:kbios
ID: 36583486
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
 
LVL 13

Expert Comment

by:themrrobert
ID: 36583533
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
 

Author Comment

by:kbios
ID: 36583593
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 150 total points
ID: 36583644
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 36583658
AJAX can use GET or POST.  Those are the HTTP methods available for making requests to a server.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 36583668
The link that @themrrobert posted is my favorite resource for AJAX info.
0
 

Author Closing Comment

by:kbios
ID: 36583699
Thanks for the help. I need to process and test the the info you provided.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now