onclick window.open没有传递php变量

时间:2015-10-21 18:11:39

标签: javascript php html

使用以下内容时,我点击提交按钮,然后打开" domain.com/bhl.php?search ="而不是像我需要的那样传递$ _GET变量。

<form action="bhl.php" method="GET">
    <input name="search" type="text" placeholder="Type here">
    <input type="submit" onclick="window.open('bhl.php?search=<?php echo $_GET['search'] ?>','targetWindow','toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=700, height=500'); return false;" />
</form>

2 个答案:

答案 0 :(得分:2)

它不起作用,因为您在页面最初加载时正在读取搜索参数的值。页面加载后PHP不运行。单击按钮时,您需要使用JavaScript设置值。

<input type="submit" onclick="window.open('bhl.php?search=' + encodeURIComponent(this.form.search.value),'targetWindow','toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=700, height=500'); return false;" />

答案 1 :(得分:0)

您无法传递PHP变量,因为页面加载时没有值。

尝试使用javascript:

发送值
<form action="bhl.php" method="GET">
    <input id="search" type="text" placeholder="Type here">
    <input type="submit" onclick="window.open('bhl.php?search='+document.getElementById('search').value,'targetWindow','toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=700, height=500'); return false;" />
</form>

虽然我定义了一个函数,然后只调用该函数,而不是使用内联javascript。