user7778602
user7778602

Reputation:

"Converting" javascript variable (String) value into PHP variable

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

Upvotes: 0

Views: 672

Answers (1)

MilanG
MilanG

Reputation: 7114

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

Upvotes: 1

Related Questions