Solved

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

Posted on 2011-09-22
14
1,627 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 83

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 109

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
 

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 83

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 83

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 83

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

838 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