2012-02-25 90 views
0

我是PHP的新手,我有一個項目需要完成,作爲創建大學網站的更大項目的一部分。不從類內部獲取POST數據

我想在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。

結果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> 

用戶類

<?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

youre缺少構造函數中的下劃線。它應該是'function __construct()',並且對於第一個輸入(名字的輸入 - 具有' 2012-02-25 16:31:50

+2

if'isset($ firstname) ''if(isset($ this-> firstname))' – 2012-02-25 16:32:31

+3

請儘量保持您的代碼結構和可讀性。我爲你編輯了你的 – Hubro 2012-02-25 16:34:49

回答

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 s的佈局被認爲是一種古老的,窮人的方法。例如,以下是使用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> 

http://jsfiddle.net/JWUC9/

+0

非常感謝你!也爲HTML提示。是的,我對HTML和PHP都很陌生,我有幾年的Java經驗,正如你正確猜測的那樣。所以我的問題是語法!但現在它的工作如此謝謝! – 2012-02-25 21:11:27