我创建了以下 xhtml 代码来接受输入,然后通过 PHP 文件运行它,然后返回所述输入:
”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css" type="text/css" />
<title>User Input Form</title>
</head>
<body>
<div class="container">
<h2>Please enter user information</h2>
<form action="display_user_info.php" method="POST">
<table>
<tr>
<td><label for="first_name">First Name:</label></td>
<td><input type="text" id="first_name" name="first_name" required="required" /></td>
</tr>
<tr>
<td><label for="last_name">Last Name:</label></td>
<td><input type="text" id="last_name" name="last_name" required="required" /></td>
</tr>
<tr>
<td><label for="email">Email:</label></td>
<td><input type="email" id="email" name="email" required="required" /></td>
</tr>
<tr>
<td><label for="phone">Phone:</label></td>
<td><input type="tel" id="phone" name="phone" required="required" /></td>
</tr>
<tr>
<td><label for="address">Address:</label></td>
<td><textarea id="address" name="address" required="required"></textarea></td>
</tr>
<tr>
<td><label for="city">City:</label></td>
<td><input type="text" id="city" name="city" required="required" /></td>
</tr>
<tr>
<td><label for="state">State:</label></td>
<td><input type="text" id="state" name="state" required="required" /></td>
</tr>
<tr>
<td><label for="zip">Zip code:</label></td>
<td><input type="text" id="zip" name="zip" required="required" /></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
以及这个 PHP 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css" type="text/css" />
<title>User Information</title>
</head>
<body>
<div class="container">
<h2>User Information</h2>
<?php
// Process form data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
// Display user information
echo "<p><strong>Name:</strong> $first_name $last_name</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Phone:</strong> $phone</p>";
echo "<p><strong>Address:</strong> $address</p>";
echo "<p><strong>City, State, Zip:</strong> $city, $state, $zip</p>";
}
?>
</div>
</body>
</html>
我已将这两个文件放入项目文件夹中,并将该文件夹放入安装 Xammpp 的“Htdocs”文件夹中。如果我单击 html 文件并提交信息,它只会显示原始 php 代码,如果我尝试在浏览器中输入 localhost,则会收到一条错误消息,指出未找到该文件。
我确保 apache 正在运行并且文件位于 htdocs 中,我还创建了一个 test.php 以查看 aapache 是否正常运行。
让我们一步一步来解决这个问题。 首先,将 HTML 文件重命名为 .php 扩展名,例如 index.php。这将告诉 Web 服务器处理文件内的 PHP 代码。 其次,请打开网络浏览器并在地址栏中输入 localhost/project/index.php。这将从 Web 服务器请求文件并执行 PHP 脚本 请注意,/project/ 是您放置文件的目录示例