简体   繁体   English

将javascript变量(字符串)值“转换”为PHP变量

[英]“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. 我正在使用window.open()方法打开一个新窗口,并使用localStorage.setItem()方法传递Javascript变量(字符串)。 In the new window that's opened, I retrive the passed variable using localStorage.getItem() method. 在打开的新窗口中,我使用localStorage.getItem()方法检索传递的变量。 Now I need this variable for an SQL query using PHP, something like "SELECT rowname from tablename WHERE id = TheJSVariable" 现在,对于使用PHP的SQL查询,我需要此变量,例如“从表名中选择行名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. 使用window.open打开时,可以将参数添加为通用的get参数。 ie: 即:

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

And then at page.php you can read param value with: 然后在page.php中,您可以使用以下命令读取param值:

$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 Window.open和通过post方法传递参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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