请帮我填写此注册表单。对我来说很明显我的 PHP 脚本中有几个错误。我的 HTML 表单如下所示:
<form action="script_1.php" method="post">
<label for="name">Username:</label>
<input type="text" name="name" />
<label for="email">Email:</label>
<input type="text" name="email" />
<input type="submit" value="Register" />
</form>
我的 php 脚本看起来像:
<?php
// Initialize session data
session_start();
/*
* If the user is already registered, display a
* message letting them know.
*/
if(isset($_SESSION['username'])) {
echo "You're already registered as" .$_SESSION[username]; // Corrected on Stack Overflow
}
// Checks if the form was submitted
else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
/*
* If both the username and email fields were filled
* out, save the username in a session variable and
* output a thank you message to the browser. To
* eliminate leading and trailing whitespace, we use
* trim() function.
*/
$uname = isset($_POST['username']) ? trim($_POST['username']) : ''; // Corrected on Stack Overflow
$email = isset($_POST['email']) ? trim($_POST['email']) : ''; // Corrected on Stack Overflow
if(!empty($uname) && !empty($email)) { // Corrected on Stack Overflow
$_SESSION['username'] = $uname;
echo "Thanks for registering! <br />",
"Username: $uname <br />",
"Email: $email <br />";
}
/*
* If the user did not fill out both fields, display
* a message letting them know that both fields are
* required for registration.
*/
else {
echo "Please fill out both fields! <br />";
}
}
// If the form was not submitted, displays the HTML form
else {
?>
<?php } ?>
我需要它来检查输入字段是否包含某些内容,然后执行操作。如果这些字段中没有插入任何内容,我还需要它来执行不同的功能。