我是PHP的新手,我有一个项目要完成,作为一个更大的项目的一部分,为大学创建一个网站。
我正在尝试在HTML页面中使用POST方法将数据发送到名为user
的类。
isset()
方法return false
,PHP将echo "notset";
。这看起来很简单,但无论我做什么,我都无法将输入的数据输入到输入框中,我不断返回“未设置”。
表单HTML
<form method="post" action="user.php">
<table cellpadding="5">
<tr>
<td><label for="firstname">Enter Firstname:</label></td>
<td><input type="text" id="firstname" name="firstname" /><br /></td>
</tr>
<tr>
<td><label for="surname">Enter Surname:</label></td>
<td><input type="text" id="surname" name="surname" /><br /></td>
</tr>
<tr>
<td> <label for="email">Email Address:</label></td>
<td><input type="text" id="email" name="email" /><br /></td>
</tr>
<tr>
<td><label for="password">Enter Password:</label></td>
<td><input type="password" id="password" name="password" /><br /></td>
</tr>
<tr>
<td><label for="renter">Re-Enter Password:</label></td>
<td><input type="password" id="renter" name="renter" /><br /></td>
</tr>
<tr>
<td><label for="address1">Address Line 1:</label></td>
<td><input type="text" id="address1" name="address1" size="35"/><br /></td>
</tr>
<tr>
<td><label for="address2">Address Line 2:</label></td><td><input type="text" id="address2" name="address2" size="35"/><br /></td>
</tr>
<tr>
<td><label for="city">City:</label></td>
<td><input type="text" id="city" name="city" /><br /></td>
</tr>
<tr>
<td><label for="postcode">Postcode:</label></td>
<td><input type="text" id="postcode" name="postcode" /><br /></td>
</tr>
<tr>
<td><label for="country">Country:</label></td>
<td><input type="text" id="country" name="country" /><br /></td>
</tr>
<tr>
<td><input type="submit" value="Submit Details" name="submit" class="button1"/></td>
</table>
</form>
我想先在页面上打印input name="firstname"
;然后我会开始担心其他一切。以下是我的class
以及我用来打印firstname
。
结果HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head
<body>
<p>thank you! user has been created</p>
<?php
$newuser = new user();
$theuser = $newuser->getfirstname();
echo $theuser;
?>
</body>
</html>
用户CLASS
<?php
class user
{
public $email;
public $firstname;
public $lastname;
public $connection;
function _construct()
{
$this->firstname= $_POST['firstname'];
$this->lastname = $_POST['lastname'];
$this->email=$_POST['email'];
}
public function getfirstname()
{
if(isset($firstname))
{
return $this->firstname;
}
else
{
return "not set";
}
}
}
?>
答案 0 :(得分:2)
正如评论者提到的那样,如果你改变了,你的代码应该有用:
if(isset($firstname)) // This isn't Java...
为:
if(isset($this->firstname)) // ... you need $this->
以及:
_construct() // Not one _
为:
__construct() // But two __
见:
<?php
class user
{
public $email;
public $firstname;
public $lastname;
public $connection;
function __construct()
{
$this->firstname = $_POST['firstname'];
$this->lastname = $_POST['lastname'];
$this->email = $_POST['email'];
}
public function getfirstname()
{
print_r($this);
if(isset($this->firstname))
{
return $this->firstname;
}
else
{
return "not set";
}
}
}
http://jfcoder.com/test/oopuser.php
修改强>
同样请注意,使用table
进行布局被认为是一种旧的,糟糕的方法。例如,下面是使用UL
和CSS的方法。这在FF和Chrome中效果很好,但在IE中却很糟糕(我认为由于某些选择器)。但是,本教程可以解释整体方法:
http://www.alistapart.com/articles/prettyaccessibleforms
<强> CSS 强>
#register {
width: 28em;
background: #ddd;
}
#register ul,
#register li {
margin: 0;
padding: 0;
}
#register li {
margin: 0 0 1px;
list-style-type: none;
background: whiteSmoke;
display: auto;
}
#register li:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
#register label {
float: left;
width: 8em;
clear: both;
}
#register input[type=text],
#register input[type=password] {
width: 12em;
}
#register input[type=text].address {
width: 100%;
}
#register label,
#register .data,
#register-submit {
padding: .5em;
margin: 0;
}
#register .data {
float: left;
margin: 0;
width: 18em;
}
#register-submit {
clear: both;
text-align: right;
}
#register hr {
height: 1px;
background: #bbb;
border: 0;
}
<强> HTML 强>
<form id="register" method="post" action="oopuser.php">
<h1>Registration Form</h1>
<ul>
<li>
<label for="firstname">Enter Firstname:</label>
<span class="data">
<input type="text" id="firstname" name="firstname" />
</span>
</li>
<li>
<label for="surname">Enter Surname:</label>
<span class="data">
<input type="text" id="surname" name="surname" />
</span>
</li>
<li>
<label for="email">Email Address:</label>
<span class="data">
<input type="text" id="email" name="email" />
</span>
</li>
<li>
<label for="password">Enter Password:</label>
<span class="data">
<input type="password" id="password" name="password" />
</span>
</li>
<li>
<label for="renter">Re-Enter Password:</label>
<span class="data">
<input type="password" id="renter" name="renter" />
</span>
</li>
<li>
<label for="address1">Address Line 1:</label>
<span class="data">
<input type="text" id="address1" name="address1" class="address"/>
</span>
</li>
<li>
<label for="address2">Address Line 2:</label>
<span class="data">
<input type="text" id="address2" name="address2" class="address"/>
</span>
</li>
<li>
<label for="city">City:</label>
<span class="data">
<input type="text" id="city" name="city" />
</span>
</li>
<li>
<label for="postcode">Postcode:</label>
<span class="data">
<input type="text" id="postcode" name="postcode" />
</span>
</li>
<li>
<label for="country">Country:</label>
<span class="data">
<input type="text" id="country" name="country" />
</span>
</li>
<li id="register-submit">
<input type="submit" value="Submit Details" name="submit"/>
<input type="reset" value="Reset" name="reset"/>
</li>
</ul>
</form>