Taylor Reed
Taylor Reed

Reputation: 355

onclick window.open not passing php variable

When using the following, I click on the submit button and it opens "domain.com/bhl.php?search=" and not passing the $_GET variable like I need.

<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>

Upvotes: 1

Views: 1422

Answers (2)

Pharm
Pharm

Reputation: 162

You cannot pass a PHP variable through since theres no value when the page loads.

Try sending the value with 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>

Although I'd define a function then just call that function, rather than using inline javascript.

Upvotes: 0

epascarello
epascarello

Reputation: 207521

It does not work because you are reading the value of the search parameter when the page has originally loaded. PHP does not run after the page load. You would need to set the value with JavaScript when the button is clicked.

<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;" />

Upvotes: 2

Related Questions