简体   繁体   English

按提交按钮后显示数据库条目

[英]Show database entries after pressing submit button

I have a page where I search for the books that an author wrote (really basic search based on an assignment I had 2 months ago). 我有一个页面可以搜索作者写的书(实际上是根据2个月前的作业进行的基本搜索)。 I choose the author from a dropdown box and after I press the "Submit" button the results should appear. 我从下拉框中选择作者,然后按“提交”按钮,结果应出现。

Here is the page's code: 这是页面的代码:

<?php

include ("includes/connections.php");

if($_POST)
{  
  if (!isset($_POST["authors"])){
          header("Location: searchAuthor.php");     
          exit;
  }

  foreach ($_POST["authors"] as $author) 
  {
      ?????????
  }
}
?>

<?php include ("includes/connections.php");
function dropdown($intIdField, $strfNameField, $strlNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
   echo "<select name=\"{$strNameOrdinal}[]\">\n";

   echo "<option value=\"NULL\">Select Value</option>\n";

   $strQuery = "SELECT $intIdField, $strfNameField, $strlNameField
               FROM $strTableName
               ORDER BY $strOrderField $strMethod";

   $rsrcResult = mysql_query($strQuery) or die(mysql_error());

   while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
      $strA = $arrayRow["$intIdField"];
      $strB = $arrayRow["$strlNameField"] . " " . $arrayRow["$strfNameField"];    
      echo "<option value=\"$strA\">$strB</option>\n";
   }

   echo "</select>";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Book Information</title>
<link href="back.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Search for Books of an Author</h1><table width="528" border="0" align="center">
    <tr>
      <td width="480"><span id="tip">*Hitting the "Search books of Author" button   without filling the fields with an asterisk will just reset the form</span></td>
    </tr>
    </table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="formBook">
  <table width="563" border="0" align="center">    
    <tr>
      <td style="text-align: right"><label for="authors">Select an Author*:</label></td>
      <td><?php dropdown("author_ID", "author_firstname", "author_lastname", "author", "author_lastname", "authors"); ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" id="submit" value="Search books of Author" /></td>
    </tr>
    <tr>
      <td><div align="left"><img src="images/buttonleft.png" alt="previous" width="70" height="70" usemap="#Previous" border="0"></div></td>
      <td><div align="right"><img src="images/buttonright.png" alt="next" width="70" height="70" usemap="#Next" border="0">
        <map name="Previous">
            <area shape="circle" coords="35,35,33" href="addSubject.php">
        </map>
        <map name="Next">
          <area shape="circle" coords="35,35,33" href="addEdition.php">
        </map>
      </div></td>
    </tr>
  </table>
</form>

</body>
</html>

As you can see I have everything inside a table (convenient for small stuff like this). 如您所见,我将所有内容放在表格中(方便用于此类小物件)。 I want when I press the submit button, the author that was chosen to be inputed in a method which will display the results from the query. 我想要按提交按钮时,选择要输入的作者,该方法将显示查询的结果。 The query will be executed in the foreach where the ?????? 该查询将在foreach中执行,?????? are. 是。 Then I want the result of the query to be used to display inside my table (by adding more rows and inserting one result in each row through a php function) the results. 然后,我希望将查询结果用于显示在表内部(通过添加更多行并通过php函数在每行中插入一个结果)来显示结果。

Is there a way to do that just with php? 有没有办法用php做到这一点? I don't know how to use Javascript just php and html. 我不知道如何仅使用php和html来使用Javascript。 Even if I have to insert the result of the query in another page and display everything there I'm ok with that. 即使我必须将查询结果插入另一页并显示所有内容,我也可以。

I haven't written the query just yet. 我还没有写查询。

Actually the foreach is there to put in a single variable each book the author has written. 实际上,foreach会将作者写的每本书都放在一个变量中。

Here's an example. 这是一个例子。 Queries/fields are bogus. 查询/字段是虚假的。

<?php

if (isset($_POST['Submit'])) {

   $strQuery = "SELECT 'field1', 'field2', 'field3'
           FROM $strTableName
           ORDER BY $strOrderField $strMethod";

$rsrcResult = mysql_query($strQuery) or die(mysql_error());

?>
<table>
<td> HEADER 1 </td> <td> HEADER 2 </td> <td> HEADER 3 </td>

<?php

 while ($row = mysql_fetch_array($rsrcResult) { 
 echo "<tr><td>".$row['field1']."</td><td>".$row['field2']."</td><td>".$row['field3']."</td>";

}

?>

</table>

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

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