PHP Search Form - results same page

Hi - I have been struggling with this for a few days and need help!!!
Basically - I have a search form & the results show on the same page.

We shall call this page page.php. Within page.php, I have pages which are pulled from a database, and so a link would be page.php?pid=2.

The issue I have is when I run the search on page.php, no problems the results come back on that page, however if I run a search on page.php?pid=2, the page goes back to page.php and shows the results there.

I am using the below for the form itself


Enter Postcode:

<?php if(isset($_GET['submit'])){ mysql_connect ("xxx", "xxx","xxx") or die (mysql_error()); mysql_select_db ("database"); $term = $_GET['term']; $sql = mysql_query("select pagetitle from table where extra like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo ''.$row['pagetitle']; echo '
'; } } ?> [/php] As I said above, the search works fine, it brings back the correct results but I just can't get it to the pid...i tried

$_GET[‘pid’] is used further up the script and calculated the correct pid…

Any help will be so so so appretiated!!!

Add this line between your tags:

<?php echo $_GET['pid']?('<input type="hidden" name="pid" value="'.$_GET['pid'].'">'):''  ?>

You can try adding the following code inside the form action:


What this will point to you script and keep the pid=2 as well. There are a few other ways to do this, but I find this the best because you can use this when doing other things. $_SERVER[‘SERVER_NAME’] lists the complete URL of the script: and $_SERVER[‘REQUEST_URI’] retrieves everything after: pid=2. Hope this helps=D.

Thanks guys :smiley: I will give those solutions a whirl!

Thanks PHP Coder, worked a treat.

Sponsor our Newsletter | Privacy Policy | Terms of Service