我的testing2.php有什么问题
这是我的php表单代码我的testing2.php有什么问题,php,forms,Php,Forms,这是我的php表单代码 <?php // Start the session session_start(); ?> <!DOCTYPE html> <html lang="en"> <head> <title>Page Title Goes Here</title> <meta charset="utf-8"> <link rel="styles
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>
</head>
<body>
<?php
// define variables and set to empty value
$firstNameError = "";
$lastNameError = "";
$error = false;
// if firstName is empty, make it NULL, else, test_input() the data.
$firstName = empty($_POST["firstName"]) ? NULL : test_input($_POST["firstName"]);
// if lastName is empty, make it NULL, else, test_input() the data.
$lastName = empty($_POST["lastName"]) ? NULL : test_input($_POST["lastName"]);
if (isset($_POST["submittingForm"])) {
/// CHECK FIRST NAME ERRORS
if ($firstName === NULL) {
// firstName is empty
$firstNameError = "First name is required!";
$error = true;
} else {
// check characters
if (!preg_match("/^[a-zA-Z ]*$/", $firstName)) {
$firstNameError = "Only letters and white spaces allowed!";
$error = true;
}
}
/// CHECK LAST NAME ERRORS
if (!preg_match("/^[a-zA-Z ]*$/", $lastName)) {
// check characters
$lastNameError = "Only letters and white spaces allowed!";
$error = true;
}
// if no error then redirect
if (!$error) {
$_SESSION['fistName'] = $firstName;
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}
} else {
// user did not submit form!
}
// clean input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="wrapper">
<h1>Welcome to Chollerton Tearoom! </h1>
<form id="userdetail" method="POST">
<fieldset id="aboutyou">
<legend id="legendauto">user information</legend>
<p>
<label for="firstName">First Name: </label>
<input type="text" name="firstName" id="firstName" value="<?php echo $firstName; ?>">
<span class="error">* <?php echo $firstNameError;?></span>
<label for="lastName">Last Name: </label>
<input type="text" name="lastName" id="lastName" value="<?php echo $lastName; ?>">
<span class="error">* <?php echo $lastNameError;?></span>
</p>
<p>
<input type="submit" name="submittingForm" value="submit">
</p>
</fieldset>
</form>
</div>
</body>
</html>
页面标题在这里
欢迎来到乔勒顿茶室!
用户信息
名字:
提交表单时,请注意:第5行F:\xampp\htdocs\en407b\assignment\testing2.php中的未定义索引:firstName将出现。。。
我试着把它修好,但还是没修好。。。
请帮助我……您有一个简单的打字错误-
if (!$error) {
$_SESSION['fistName'] = $firstName;
应该是:
if (!$error) {
$_SESSION['firstName'] = $firstName;
我还建议您在表单中执行以下操作:
<form id="userdetail" action="testing2.php" method="POST">
然后将所有表单处理逻辑移到该文件中。这将消除头重定向的需要。会话名称中有输入错误。您正在将会话分配给
$\u会话['fistName']
,并尝试使用$\u会话['firstName']访问它代码>像这样更改
if (!$error) {
$_SESSION['firstName'] = $firstName;//error here
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}
而且。。。。如果lastname未填充,则它将自动分配给null。。。。提交后,testing2.php中出现黑色。。。成功提交:fitstName:asdsada lastName:如何修复???先生。。。如果lastName为空,则lastName不会在testing2.php页面中自动分配单词“NULL”。。。如何修复它?更改echo“lastName:$lastName”;在将2.php测试为if($_SESSION['lastName']==NULL){echo“firstName:NULL”}或{echo”lastName:$lastName“;}的过程中,这是一种简单的方法。。。如果lastName为空,则lastName不会在testing2.php页面中自动分配单词“NULL”。。。我怎样才能修好它?
if (!$error) {
$_SESSION['firstName'] = $firstName;//error here
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}