【发布时间】:2016-11-21 15:20:22
【问题描述】:
我一直在尝试将表单提交中的数据写入 PHP 类对象。我正在使用以下基本提交表单。
<html>
<body>
<h2>Form Submit</h2>
<form action='object_of_array2.php' method='POST'/>
<input type="text" name='first_name' value=''/><br/>
<input type="text" name='last_name' value=''/><br/>
<input type="text" name='email_address' value=''/><br/>
<input type='Submit' name='submit' value='GO'/>
</form>
</html>
</body>
我找到了之前的一篇文章,How to grab data from post to a class。这适用于我的提交表单。
<?PHP
class RegisterUser {
private $firstName;
private $lastName;
private $emailAddress;
function __construct() {
$this->firstName = isset($_POST['first_name']) ? $_POST['first_name'] : null;
$this->lastName = isset($_POST['last_name']) ? $_POST['last_name'] : null;
$this->emailAddress = isset($_POST['email_address']) ? $_POST['email_address'] : null;
}
function start() {
if (empty($this->firstName) || empty($this->lastName) || empty($this->emailAddress)) {
echo "Empty Post not allowed";
}
else
{
// Do some stuiff
echo " Registration Done";
}
}
}
$register = new RegisterUser();
if(!empty($_POST))
{
$register->start();
}
?>
我正在尝试找出如何回显 $firstname、$lastname 和 $emailaddress。如何访问这些以在其他地方使用?
【问题讨论】:
-
“我怎样才能访问这些以在其他地方使用?” - 你将不得不详细说明这一点。
-
这是非常琐碎的东西;只是回应成功。我会发布一个答案,但不知道你想用它来做什么,这让我犹豫了。你对我的cmets的沉默也增加了我的犹豫。我希望你喜欢阅读我给你的那些链接,里面有很多好东西。
-
感谢 Fred,我指的是使用变量写入 MySQL。
-
欢迎...只需在您的
// Do some stuiff中回显$this->firstName等,然后插入到数据库中,如果您想这样做。可能还有其他方法,但这是快速而肮脏的方法。