Solved

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

Posted on 2011-09-22
14
1,637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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 110

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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?
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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