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

3
  • How are you sending the query to PHP? Using Ajax or a form? Commented Apr 24, 2017 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) Commented Apr 24, 2017 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) Commented Apr 24, 2017 at 11:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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?
updated my answer, and yes, window.open should be generated something like you wrote in your comment
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? :)
I think that you can pass get parameters with both. Google is your friend! :)
Got it! Thanks mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.