简体   繁体   中英

Loading php form search results on page load

I have a simple php form that allows users to choose from a drop-down list of subject areas to return a set of databases (ex. http://library.wabash.edu/biology.php ). As you can see, nothing is returned until a selection is made and Submit is clicked. Is it possible to have a set of databases already load when the page loads (biology databases would load on the Biology Dept page, etc.) but then also allow the users to make another selection, as I have now? My inexperience shows here, so apologies in advance.

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
   <select name="choice" id="choice">
      <option value="Biology">Biology</option>
      <option value="Chemistry">Chemistry</option>
      <option value="Computer Science">Computer Science</option>
      <option value="Mathematics">Mathematics</option>
      <option value="Medicine">Medicine</option>
      <option value="Physics">Physics</option>
</select>

   <input type="submit" name="submit" value="Go" style="margin-left: 10px">
   <input type="submit" name="reset" value="Clear" style="margin-left: 10px">
   <input type="hidden" name="submitted" value="true"/>
</form>

<?php
if(isset($_POST['submit']))   {

   $choice=$_POST['choice'];

   $localhost="localhost";
   $username="xxxxxxx";
   $password="xxxxxxx";
   $database="xxxxxxx";

   $linkid=mysql_connect($localhost,$username,$password);  
   @mysql_select_db($database) or die( "Unable to select database");

   mysql_select_db("databases",$linkid);
   $resultid=mysql_query("SELECT  name, mobile, app, tutorial, help
                          FROM databaselist
                          WHERE dept
                          LIKE '%{$choice}%'
                          ORDER BY sortname ASC", $linkid);

   echo"<table>";
   while ($row = mysql_fetch_row($resultid))
      {
         echo"<tr>";
         foreach ($row as $field)
            {
               echo"<td>$field</td>";
            }
         echo"</tr>";
      }
   echo"</table>";
   mysql_close($linkid);
}
?>

As you have now if u don't intend to use javascript, you can do one thing:

1- remove the validation of $_POST['submit'], you can check only for the $choice=$_POST['choice'] variable. 2- change the choice variable from $_POST array to $_GET array, that way you can build a link something like. --> mypage.com/index.php?choice=Biology. having this you will be able to fill the list on the biology page for example, and also let the user choose any other choice.

If you so want, you can preload all the data for different subjects and save it into Javascript Variables so that accessing them is just a matter of waiting for the SELECT change event trigger.

Or you can use Ajax to load the relevant subject variables. Same way only that it's the server side code that serves up the data instead of that in stored Javascript Variables.

From your question it seems you just want a pre-loaded list of resources to populate the page when the visitor first lands (perhaps just to fill the space?). As a basic PHP solution, just run an initial query at the top of your page, limited to, say, the first 100* alphabethically placed records and echo them out in your table below the form.

Don't forget to wrap that initial query in an if statement, eg:

if(!ISSET($_POST)) {
    //run query
}

*(If you're paginating the results, obviously you don't need 100 - just however many to fill that first screen.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM