2

我有一个页面,我可以在其中搜索作者写的书(基于我 2 个月前的作业进行的基本搜索)。我从下拉框中选择作者,然后按“提交”按钮,结果应该会出现。

这是页面的代码:

<?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>

正如你所看到的,我把所有东西都放在一张桌子里(对于这样的小东西很方便)。我希望当我按下提交按钮时,选择要输入的作者将显示查询结果的方法。查询会在foreach哪里执行??????是。然后我希望查询的结果用于在我的表中显示(通过添加更多行并通过 php 函数在每一行中插入一个结果)结果。

有没有办法只用php做到这一点?我不知道如何使用 Javascript 只是 php 和 html。即使我必须在另一个页面中插入查询结果并在那里显示所有内容,我也可以接受。

我还没有写查询。

实际上,foreach 是在作者写的每本书中放入一个变量。

4

1 回答 1

1

这是一个例子。查询/字段是虚假的。

<?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>
于 2012-06-16T13:21:55.877 回答