简体   繁体   中英

“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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM