0

I am using window.open() method to open a new window and passing a Javascript variable (String) using localStorage.setItem() method. In the new window that's opened, I retrive the passed variable using localStorage.getItem() method. Now I need this variable for an SQL query using PHP, something like "SELECT rowname from tablename WHERE id = TheJSVariable"

But I couldn't find any where a solution for it.

Any suggestions? Thanks

  • How are you sending the query to PHP? Using Ajax or a form? – Manngo Apr 24 '17 at 11:52
  • send this value via ajax to the same page or to the different php page and using $_POST get that value (use ajax post method) – Alive to die - Anant Apr 24 '17 at 11:52
  • Manngo: I am not using Ajax or a form, only a simple query and then mysqli_query to fetch results. Alive to Die: How can I send a Javascript string value using ajax post in a window.open method? Any link? thanks (I'm new to this) –  Apr 24 '17 at 11:57

1 Answers1

1

When opening with window.open you can add your parameter as common get parameter. i.e.:

window.open ("mysite.com/page.php?param=123");

And then at page.php you can read param value with:

$param = $_GET['param'];

update:

If you want to make post request you could add (hidden) form with form fields holding the values you want to pass and submit that form from code. Something like:

<form id="TheForm" method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something" />
<input type="hidden" name="more" value="something" />
<input type="hidden" name="other" value="something" />
</form>

<script type="text/javascript">
window.open('', 'TheWindow');
document.getElementById('TheForm').submit();
</script>

Window.open and pass parameters by post method

Community
  • 1
  • 1
MilanG
  • 6,994
  • 2
  • 35
  • 64
  • Thank you, and a few questions: (1)If the param is a variable, i.e var x = "String"; Should I type window.open ("mysite.com/page.php?param=" + x);? (2)If I want a POST instead of GET, what would be the way to write it using window.open? –  Apr 24 '17 at 12:00
  • updated my answer, and yes, window.open should be generated something like you wrote in your comment – MilanG Apr 24 '17 at 12:04
  • Thank you! By the way, my mistake, I have been using window.location instead of window.open, so I should switch all window.location to window.open, right? because window.location does not have the capability of adding parameters? Or does it? :) –  Apr 24 '17 at 12:16
  • I think that you can pass get parameters with both. Google is your friend! :) – MilanG Apr 24 '17 at 13:29
  • Got it! Thanks mate! –  Apr 25 '17 at 07:33