PHP Form - Submit On Page Load

Hi All,

I am trying to figure this out but no luck!!! I have a form that brings back a list of companies (searchable by postcode). When a user first hits the page (…/page.php) no results show until the user hits ‘Go’
What I am trying to do is have the form submit once the page has loaded so it shows a list of all companies. PHP code is below and it doesnt work, IE shows script errors.


Search for a Restaurant in your area! Enter your Postcode Area (e.g. BS1):

<?php echo $_GET['pid']?(''):'' ?>
<?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 Restaurant where extra like '%$term%' and showing like '1'");

while ($row = mysql_fetch_array($sql)){
echo ‘’.$row[‘pagetitle’];
echo ‘




Any help will be real appretiated.


how will the form know what postcode to search by if its not submitted through the form?

Once you figured that bit out then just take the query out the if(submitted clause so it just runs,
IE: get rid of this [php]if(isset($_GET[‘submit’])){[/php] and this [php]}[/php] but,
unless there is a value it can search with, then it will show nothing except maybe some errors.

Red. :wink:

If it’s empty it pulls back all results (which is what is required as soon as the page loads!)

However if someone enters a search query, i also want that to show…

in that case you use an if/else for your query:
// create a query to search by postcode
$query = “SELECT * FROM database WHERE postcode=’{$_GET[‘submit’]}’”;
// create a query to collect all records
$query = “SELECT * FROM database”;
// run whichever query is set
$result = mysql_query($query);

and then use the while loop you already have to print out the results.

Hope that helps


Sponsor our Newsletter | Privacy Policy | Terms of Service