我只是重新学习PHP,除了这次是OOP。
所以我正在上课/功能课,我决定尝试用户输入的功能并回复该信息。
我的代码:
<?php
$f_name = $_POST['firstname'];
$l_name = $_POST['lastname'];
$agee = $_POST['age'];
$name = new Person($f_name, $l_name, $agee);
class Person {
var $first_name;
var $last_name;
var $age;
function __construct($firstName, $lastName, $myAge) {
$this->first_name = $firstName;
$this->last_name = $lastName;
$this->age = $myAge;
echo "My name is $firstName $lastName, and my I'm $myAge years old.";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inserting</title>
</head>
<body>
<form action="enteredfunction.php" method="post" autocomplete="off">
<input type="text" name="firstname" placeholder="First name">
<input type="text" name="lastname" placeholder="Last name">
<input type="text" name="age" placeholder="Age">
<input type="submit" value="Insert">
</form>
</body>
</html>
我收到以下错误:
Notice: Undefined index: firstname in C:\xampp\htdocs\ooptutorial\classes\enteredfunction.php on line 3
Notice: Undefined index: lastname in C:\xampp\htdocs\ooptutorial\classes\enteredfunction.php on line 4
Notice: Undefined index: age in C:\xampp\htdocs\ooptutorial\classes\enteredfunction.php on line 5
My name is , and my I'm years old.
我知道我的错误是什么,但我不知道该怎么做。
任何帮助都将不胜感激。
答案 0 :(得分:2)
先检查它是否为空
if (
!empty($_POST['firstname']) &&
!empty($_POST['lastname']) &&
!empty($_POST['agee'])
){
$f_name = $_POST['firstname'];
$l_name = $_POST['lastname'];
$agee = $_POST['age'];
$name = new Person($f_name, $l_name, $agee);
}
答案 1 :(得分:0)
您的问题是您所寻找的$_POST
值不存在。尝试做var_dump($_POST)
并看看那里有什么。
另外,请尝试确保表单上的action属性包含其所在文件的名称或尝试将其留空。