0

I have a value stored in localstorage like this:

window.localStorage.setItem('name', document.getElementById("name_text").value);

When query the table, I want go query like this:

$query = "SELECT * FROM table WHERE user= "

I want to select from my table, where the user is equal to the value stored in localStorage. How can I do this?

I tried the following but no get no info from the table:

<script>
    var keyName = window.localStorage.getItem('name');
</script>

<?php
      $query = "SELECT * FROM table WHERE name='.$keyName.'"
  ?>

Test 1:

<script>
var name = window.localStorage.getItem('name');
$.ajax({
  url: "index.php",
  method: "POST",
  data: { "name": name }
})
</script>

<?php

      $name = $_POST['name'];
      echo($name);
      // prints 1

      $query = "SELECT * FROM table WHERE name='$name'";
      ?>
  • Welcome to Stack Overflow! Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output using the `[<>]` snippet editor. – mplungjan Jul 10 '20 at 08:41
  • @mplungjan Updated my question to what I have tried, maybe you can look at that and tell me what I am missing? –  Jul 10 '20 at 08:44
  • PHP is on the server and JS is on the client. Please look at dupe and the links in the search I gave you – mplungjan Jul 10 '20 at 08:44
  • Also https://techdifferences.com/difference-between-server-side-scripting-and-client-side-scripting.html#:~:text=Server%2Dside%20scripting%20is%20used,can%20see%20from%20the%20browser. – mplungjan Jul 10 '20 at 08:45
  • @mplungjan I cant figure out why my code doesn't work. It looks fine to me. –  Jul 10 '20 at 08:50
  • Php cannot read js vars – mplungjan Jul 10 '20 at 08:52
  • @mplungjan Hm, then I'm not sure how to proceed. Are you sitting on a solution that you might share please? Or are you also unsure? –  Jul 10 '20 at 09:14
  • No any of the dupes posted should be studied by you!! – mplungjan Jul 10 '20 at 09:18
  • @mplungjan Ok, maybe someone else knows and can help me find a solution. –  Jul 10 '20 at 09:27
  • @mplungjan I am new, and I am trying to learn. I have read the links you sent me, but I still can't figure out, because I am new to this. Understand that. –  Jul 10 '20 at 09:34
  • Is THIS a better example? https://stackoverflow.com/questions/51336332/how-to-take-javascript-variable-in-php-mysql-query – mplungjan Jul 10 '20 at 09:36
  • @mplungjan Ok, I think I am on to something now? Can you please check my `Test 1` in my updates answer, please? :) –  Jul 10 '20 at 09:52
  • Looks ok. I would test to see if name===null before bothering the backend – mplungjan Jul 10 '20 at 09:54
  • @mplungjan I looks like it returns null, because I get no value in the table. I also get error on line `$name = $_POST['name'];` Message: `Notice: Undefined index: name`. –  Jul 10 '20 at 09:57
  • So start with `var name = "known name in db"` - and index is an SQL issue, no? – mplungjan Jul 10 '20 at 09:59
  • Like: `if(isset($_POST['name'])) { $name = $_POST['name']; }` – mplungjan Jul 10 '20 at 10:01
  • @mplungjan I changed it like this: `var name = "John"`, and I know that there are multiple cell in name column that contains the name John. But still no output, and error `Notice: Undefined variable: name` on line `$query = "SELECT * FROM table WHERE name='$name'";` –  Jul 10 '20 at 10:24
  • `if(isset($_POST['name'])) { $name = $_POST['name']; } else { echo "name not passed"; die; }` – mplungjan Jul 10 '20 at 10:37
  • @mplungjan Ah, yea.. It gives me `name not passed`. –  Jul 10 '20 at 11:13
  • try this: `const name = window.localStorage.getItem('name'); if (name) $.post("index.php",{name:name},function(response) { console.log(response) }); else {console.log("name not in localstorage");}` and have your php return the result using echo – mplungjan Jul 10 '20 at 11:17
  • @mplungjan Do you know why it gives me: `Syntax error, unexpected '.', expecting error on T_VARIABLE or '{' or '$'` ? –  Jul 10 '20 at 11:23
  • I posted JavaScript, not PHP - replacement for your $.ajax – mplungjan Jul 10 '20 at 11:24
  • @mplungjan So like this: https://pastebin.com/Zrwc2D1u? Still getting this `Notice: Undefined variable: name in`. I just can't understand why it can't get the name variable... –  Jul 10 '20 at 11:32
  • Where did that "echo" come from, I mean echo the result! Something like this https://pastebin.com/idBk1ZRQ – mplungjan Jul 10 '20 at 11:53
  • @mplungjan Ugh, I still get the `name not passed` tho. I have checked in my browser (Chrome) using F12->Application, and the localstorage key is there. Is there anything else I need to do? Btw; All the code in running in the same file `ìndex.php`. –  Jul 10 '20 at 12:04
  • Does not make any sense. Perhaps you need to refresh the browser cache – mplungjan Jul 10 '20 at 12:23
  • @mplungjan I even tried using another browser(Microsoft Edge), but still gives the same message (And yes, I also checked that the key was saved in Edge). –  Jul 10 '20 at 12:34
  • I need to see your complete code running on a site somewhere – mplungjan Jul 10 '20 at 12:35
  • @mplungjan Here is my `index.php` code: https://pastebin.com/PLKthZdu - I've had to remove a few thing because of GDPR, but this is pretty much all the code. –  Jul 10 '20 at 12:43
  • You need to match my changes to your code. I changed from $query to $sql and looped over the result with a br - you already HAVE code using the result. Don't just blindly paste my code into your code. I do not have a clue why you get the error you get. Can you just try a completely minimal version that just echos the name back? – mplungjan Jul 10 '20 at 12:48
  • @mplungjan Ok, I made a minimal version to test, like this: https://pastebin.com/vxWHYvdi. (I changed key-name to `tcad` to try something else as you can see This is what my website looks like, and view that the key exist, but still won't pass. https://ibb.co/zb3xjKK –  Jul 10 '20 at 13:06
  • https://plungjan.name/SO/testname/ works for me now - I would not use the same php to run client and server. I would have another php do the sql https://pastebin.com/dmYttDNn – mplungjan Jul 10 '20 at 13:23
  • @mplungjan Wow, you made it work using the same php also. Do you recommend to have two different php? index.php and localstorage.php ? –  Jul 10 '20 at 13:32
  • index.php and getdata.php :) – mplungjan Jul 10 '20 at 13:33
  • @mplungjan Ok, besides I have no idea what to put where, and how to connect it. :) I'm pretty new to this as your might already understand, hehe. But how come when I use your code in my php file, it won't work? But it works when using only your code? –  Jul 10 '20 at 13:40
  • Likely because we use different names for stuff. You need to watch very carefully where your code is not the same as mine. I do not have a database on my system so I cannot test the version you have with the loop – mplungjan Jul 10 '20 at 13:43
  • @mplungjan Ah, yea! I added a few lines on top and bottom, and this one: ``, and now I got popup: `Resonse from PHP: IML08768`. Cool! But why does it still says `name not passed` on my page? –  Jul 10 '20 at 13:52
  • Maybe there is nothing in localStorage under that variable name – mplungjan Jul 10 '20 at 13:53
  • @mplungjan But there is? As I am writing this, the localStorage key is `tcad` and value `IML08768`. –  Jul 10 '20 at 13:57
  • I cannot tell you without looking – mplungjan Jul 10 '20 at 13:58
  • @mplungjan What do you want me to show you? –  Jul 10 '20 at 13:58
  • If you can share your test page - my contact info is on my site which is in my profile – mplungjan Jul 10 '20 at 13:59
  • So where do you SET something in localStorage? – mplungjan Jul 10 '20 at 14:29
  • @mplungjan Oh, sorry! Updated my test page now. You set something in localStorage by pressing the button, the values comes from the input text. So press button to save, and reload page to display the value. –  Jul 10 '20 at 14:57
  • @mplungjan Oh :/ ... –  Jul 10 '20 at 15:07
  • @mplungjan Well, I do appreciate your help, and I hope I'll find a solution soon :) –  Jul 10 '20 at 15:10

0 Answers0