使用 HTML 的 PHP 表格

2023-11-29

我的目标是使用一个 txt 文件,其中包含填写表单的特定响应所需的数据。该表格应该有用户名和密码。此外,还可以在表单上设置价格范围参数。用户名可以任意大小写,但密码区分大小写。我已经创建了我将使用的 html 表单。 txt 文件中的数据包含列表地址和列表价格。价格范围参数可以设置为任何值,目标是仅返回 txt 文件中的数据作为完成表单的生成响应。我创建的表格是,

<!DOCTYPE html>

<html>
<head>
<title></title>
</head>
<body>
<form action="houseprices.txt"
      method="post">
Show houses whose price is between:
<input name="lowprice" type="text" size="15" maxlength="15" /> & 
<input name="highprice" type="text" size="15" maxlength="15" /><br />
Username:   <input name="user" type="text" size="15" maxlength="15" /><br />
Password:   <input name="pw" type="password" size="15" maxlength="15" /><br />
<input type="submit" value="Show Houses" />
</form>
</body>
</html>

唯一有效的用户名是

用户名:joe [任何情况(jOE)]

密码:tiger [区分大小写]

当我创建响应文件时,我应该使用 php 语法还是使用 html 并在 html 文档中插入 php 编码。我正在使用 sublime,这就是为什么我问要使用哪种语法。当表单返回一页时,信息必须放置在两列表格中。我还没有开始编写响应页面代码,我希望获得一些关于如何构建所需代码的帮助。必须返回的文本文件是这个。

$houseprices["123 Elm"]=260000;
$houseprices["96 Otis"]=340000;
$houseprices["9 Windham Place"]=560000;
$houseprices["293 Wilma"]=230000;
$houseprices["6789 Beach"]=800000;
$houseprices["535 Overlook"]=750000;
$houseprices["59 South Lake"]=900000;
$houseprices["19 Ash"]=270000;
$houseprices["6820 Butternut"]=340000;
$houseprices["902 Weston"]=190000;
$houseprices["496 Ervin"]=340000;
$houseprices["316 Pond"]=280000;
$houseprices["7282 Main"]=420000;
$houseprices["561 Billings"]=350000;
$houseprices["117 Peach"]=280000;
$houseprices["2171 Bentley"]=300000;
$houseprices["365 Prescott"]=1120000;
$houseprices["319 Paley"]=310000;
$houseprices["43 Maple"]=150000;
$houseprices["291 Wilson"]=320000;
$houseprices["81 Essex"]=330000;
$houseprices["995 West Lawrence"]=480000;
$houseprices["679 Esther"]=240000;
$houseprices["6890 Patten"]=600000;
$houseprices["53 Lower Pond"]=250000;
$houseprices["4567 Washington"]=170000;
$houseprices["691 Holt"]=1900000;
$houseprices["1234 Main"]=180000;
$houseprices["6 Cherry"]=360000;
$houseprices["639 Perrywinkle"]=190000;
$houseprices["86 Foster"]=1300000;
$houseprices["341 Elm   "]=200000;
$houseprices["5122 Bern"]=350000;
$houseprices["688 Maple"]=210000;
$houseprices["64 Oak"]=640000;
$houseprices["874 Marlin"]=220000;
$houseprices["53 Lake"]=190000;
$houseprices["206 Alban"]=310000;
$houseprices["371 Martha"]=420000;
$houseprices["866 Delaware"]=230000;
$houseprices["342 West Lawrence"]=440000;
$houseprices["63 Ashley"]=450000;
$houseprices["49 Tabler"]=260000;
$houseprices["417 Deermill"]=370000;
$houseprices["38 Holt"]=490000;
$houseprices["689 Aspen"]=300000;
$houseprices["4441 Park"]=210000;
$houseprices["8394 North Lake"]=320000;
$houseprices["722 Perry"]=530000;
$houseprices["99 East Ridge"]=260000;
$houseprices["4374 Elderberry"]=270000;
$houseprices["89 Lubbock"]=370000;
$houseprices["12543 Benson"]=380000;
$houseprices["240 London"]=290000;
$houseprices["745 Park"]=390000;
$houseprices["18 Wilson"]=400000;
$houseprices["11 Westgate"]=540000;
$houseprices["4736 East River"]=250000;
$houseprices["4793 Chauncey Circle"]=370000;
$houseprices["3073 West Main"]=280000;

笔记: • 正确的用户名是joe,正确的密码是tiger。用户名可以在任何情况下输入,并且后面可以有前导或尾随空格。但是,密码必须全部小写,且后面不能有空格。

• 如果用户没有输入正确的用户名和密码,他或她将收到一个h3 标题的网页,其中包含一条消息“用户名或密码无效”。

• 如果用户输入正确的用户名和密码,他或她将收到一个网页,其中列出了指定价格之间的所有房屋的表格。 (介于之间意味着大于或等于较小的数字且小于或等于较高的数字。)在此网页的末尾将是表单的副本,以便用户可以输入另一个请求。

• PHP 输出的格式应如示例所示。 “这里是价格在 100000 美元到 500000 美元之间的所有房屋”这句话应位于表格内的 h3 标题中(美元符号应打印在所示数字之前)。表格的边框应为 2 像素的实心红色边框,如图所示。标题“再试一次”应该是 h3 标题。

<?php
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "<title>Houses</title>";
echo "</head>";
echo "<body>";
echo '<h3> Here are all the houses whose prices are between $100000 and $500000: </h3>';


echo "</body>";
echo "</html>";
?>

我首先将您的表单发布到与表单相同的页面(以便您可以重新使用该表单)。

然后,您需要放入一些 PHP 逻辑来显示数据(如果有)或无效登录的错误。

要显示房屋,您可以包含文本文件,然后运行数组并查找所需范围内的任何内容。您可以使用 foreach 循环。

<!DOCTYPE html>
<html>
<head>
  <title>Your Form Page</title>
</head>
<body>
<!-- Your HTML STUFF BEFORE WHERE YOU WANT THE TABLE TO BE -->


<?php  // This tag opens up PHP
//Check to see if username & password are correct
if(strcmp($_POST['username]'], 'joe') === 0 && strcasecmp($_POST['password'], 'tiger') === 0) {

  // Include the array from your text file -- as you've structured it as PHP
  require('textfile.txt');

  // Start the table
  echo '<table><tr><th>Address</th><th>Price</th></tr>';

  foreach($houseprices as $address => $price) {
    if($price >= $_POST['lowprice'] && $price <= $_POST['highprice']){
      echo '<tr><td>' . $address . '</td><td>$' . $price . '</td></tr>';
    }
  }

  // Close out the table
  echo '</table>';

} else {
  // Display warning about password/username
  echo '<h2>Incorrect Username or Password</h2>';
}

// The tag below will close php
?>

<!-- The rest of your HTML goes down here -->
<form> ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 HTML 的 PHP 表格 的相关文章