简体   繁体   English

在页面加载时加载php表单搜索结果

[英]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 ). 我有一个简单的PHP表单,允许用户从主题区域的下拉列表中选择返回一组数据库(例如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: 正如你现在所知,如果你不打算使用javascript,你可以做一件事:

1- remove the validation of $_POST['submit'], you can check only for the $choice=$_POST['choice'] variable. 1-删除$ _POST ['submit']的验证,你只能检查$ choice = $ _ POST ['choice']变量。 2- change the choice variable from $_POST array to $_GET array, that way you can build a link something like. 2-将选择变量从$ _POST数组更改为$ _GET数组,这样就可以构建类似的链接。 --> mypage.com/index.php?choice=Biology. - > 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. 如果您愿意,可以预加载不同主题的所有数据并将其保存到Javascript变量中,以便访问它们只需等待SELECT更改事件触发器。

Or you can use Ajax to load the relevant subject variables. 或者您可以使用Ajax加载相关的主题变量。 Same way only that it's the server side code that serves up the data instead of that in stored Javascript Variables. 同样的方式只是服务器端代码提供数据而不是存储的Javascript变量。

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. 作为一个基本的PHP解决方案,只需在页面顶部运行一个初始查询,仅限于,例如,前100 *字母顺序放置的记录,并在表格下方的表格中回显它们。

Don't forget to wrap that initial query in an if statement, eg: 不要忘记将该初始查询包装在if语句中,例如:

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.) *(如果您对结果进行分页,显然您不需要100 - 只需要多少填充第一个屏幕。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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