2017-04-24 109 views
0

我使用window.open()方法打开一个新窗口并使用localStorage.setItem()方法传递一个Javascript变量(String)。 在打开的新窗口中,使用localStorage.getItem()方法检索传递的变量。 现在我需要这个变量的SQL查询使用PHP, 类似于“SELECT rowname from tablename WHERE id = TheJSVariable”将JavaScript变量(String)值转换为PHP变量

但我找不到任何解决方案。

有什么建议吗? 谢谢

+0

你是如何将查询发送到PHP?使用Ajax或表单? – Manngo

+0

通过ajax发送此值到同一页面或不同的php页面并使用$ _POST获取该值(使用ajax post方法) –

+0

Manngo:我不使用Ajax或表单,只使用简单的查询,然后使用mysqli_query获取结果。 活着死:我怎样才能在window.open方法中使用ajax post发送Javascript字符串值?任何链接?谢谢(我是新手) – Foobarer

回答

1

当用window.open打开时,你可以添加你的参数作为通用获取参数。即:

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

然后在page.php文件就可以读取参数值的方式:

$param = $_GET['param']; 

更新:

如果你想POST请求,您可以添加(隐藏)表格形式字段包含您想要传递的值并从代码提交该表单。喜欢的东西:

<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

+0

谢谢,以及几个问题: (1)如果param是一个变量,即var x =“String”;我应该输入window.open(“mysite.com/page.php?param=”+ x);? (2)如果我想要一个POST而不是GET,那么使用window.open编写它的方式是什么? – Foobarer

+0

更新了我的答案,是的,window.open应该生成类似于您在评论中所写的内容 – MilanG

+0

谢谢! 顺便说一下,我的错误,我一直使用window.location而不是window.open,所以我应该将所有window.location切换到window.open,对吧?因为window.location没有能力添加参数?还是呢? :) – Foobarer