2013-04-28 77 views
0

我在項目上使用了三個文件:index.php,class.phpstyles.css表單提交 - 使用PHP類中的函數處理表單

我所有的功能駐留在PHP類中的class.php文件:

<?php 

class Club 
{ 
    //Private Data 
    private $HostName;  //typically "localhost" 
    private $UserId; 
    private $Password; 
    private $DBName; 
    private $Con;   //MySQL Connection 
    //Public Methods 
    //Constructor 
    public function __construct($host = NULL, $uid = NULL, $pw = NULL, $db = NULL) 
    { 
     $this->HostName = $host; 
     $this->UserID = $uid; 
     $this->Password = $pw; 
     $this->DBName = $db; 

     //Connect to Database 
     $this -> Con = mysqli_connect($host, $uid, $pw, $db); 
     if(mysgli_connect_errno($this -> Con)) 
     { 
      echo "Failed to connect to MySQL; " . mysqli_connect_error(); 
     } 

    } 
    //Destructor 
    public function __destruct() 
    { 
     //Close connection 
     mysqli_close($this -> Con); 
    } 
    public function DisplayMembers() 
    { 
      } 
      function DisplayAbout() 
    { 
      } 
      function DisplayRegistrationForm() 
    { 
     echo("<h2>Become a Club Member</h2> 

     <form name='register' method='post' action=''> 
      <table> 
       <tbody> 
        <tr> 
         <td width='80px'> 
          <label>First Name: </label> 
         </td> 
         <td width='300'> 
          <input id='firstname' type='text' name='firstname' value='' required/> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label>Last Name: </label> 
         </td> 
         <td> 
          <input id='lastname' type='text' name='lastname' value='' required/> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label>Your Email: </label> 
         </td> 
         <td> 
          <input id='email' type='text' name='email' value='' required/> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label>Gender: </label> 
         </td> 
         <td> 
          <input id='gender' type='radio' name='gender' value='male'>Male<br /> 
          <input id='gender' type='radio' name='gender' value='female'>Female 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <label>Interested in: </label> 
         </td> 
         <td id='check'> 
          <span style='font-weight: bold;'>Check All that Apply:</span><br /> 
          <input id='interests' type='checkbox' name='interests[]' value='Pizza Party'>Pizza Party<br /> 
          <input id='interests' type='checkbox' name='interests[]' value='Joining Study Groups'>Joining Study Groups<br /> 
          <input id='interests' type='checkbox' name='interests[]' value='Visiting Employer Sites'>Visiting Employer Sites<br /> 
          <input id='interests' type='checkbox' name='interests[]' value='Participating in Programming Competition'>Participating in Programming Competitions<br /> 
          <input id='interests' type='checkbox' name='interests[]' value='Building Games'>Building Games<br /> 
          <input id='interests' type='checkbox' name='interests[]' value='Becoming an Officer of the Club'>Becoming an Officer of the Club 
         </td> 
        </tr> 
        <tr> 
         <td colspan='2' style='text-align: center;'> 
          <input id='submit' type='submit' name='submit' value='Sign Up'/> 
         </td> 
        </tr> 
       </tbody> 
      </table>  
     </form>"); 
    } 
      function ProcessRegistrationForm() 
    { 
      } 
      private function Get_Members_From_DB() 
    { 
      } 
      private function Get_Members_Interests_From_DB($MemberEmail) 
    { 
      } 
      private function Get_Interests_Types_From_DB() 
    { 
      } 
} 
?> 

我不能爲我的生活弄清楚放什麼形式部分的操作部分,以便使用班內的ProcessRegistrationFunction()。所以基本上,DisplayRegistrationForm()index.php文件中調用div,然後我想讓它在類中由ProcessRegistrationFunction()處理。但是,我似乎無法弄清楚如何做到這一點。

+1

那麼,首先它是'__construct'而不是'_construct' – elclanrs 2013-04-28 23:38:39

+0

你在哪裏調用'DisplayRegistrationForm'? – 2013-04-28 23:38:51

+0

我在index.php中調用DisplayRegistrationForm。 – charlwillia6 2013-04-28 23:53:08

回答

2

你可以發送你的表單數據到index.php本身,併發送任何數據到你的過程函數。實現此目的的最佳方式是更改表單以將數據作爲數組收集。

這裏myFormArray將存儲這個值可以在你的index.php訪問形式

<input id='firstname' type='text' name='myFormArray[firstname]' value='' required/> 

<input id='lastname' type='text' name='myFormArray[lastname]' value='' required/> 

的所有信息,當你在你的index.php提交表單

所以

if(isset($_POST["submit"]){ 
    $formData = $_POST["myFormArray"]; // dont forget to sanitize any post data 
    //than you can call your class function and pass this data 
    $class = new class(); 
    $class->ProcessRegistrationFunction($formData); 
} 

您的ProcessRegistrationFunction($ data)現在將擁有數組形式的所有數據。

你可以遍歷它和FET的各個值

注:有很多語法錯誤在你的類..也有實現這個類的更好的方法。對於一個你應該總是避免裏面的html代碼你班組長..

DINS

+0

那麼我要把任何東西放到行動領域? – charlwillia6 2013-04-29 00:17:15

+0

你可以把index.php放在空白處..在這兩種情況下它都會提交到index.php – Dinesh 2013-04-29 00:20:52

+0

if語句給了我一個致命的代碼,無論我把它放在哪裏:致命錯誤:不能使用函數返回值在寫上下文 – charlwillia6 2013-04-29 00:21:59

0

,同時輸入正確的類型,以便u能避免不必要的錯誤

'if(mysgli_connect_errno($this -> Con))' 

應該

'if(mysqli_connect_errno($this -> Con))' 
+0

這不是一個答案。它應該在評論或編輯請求中。如果你沒有/沒有足夠的代表,那好吧。 – trysis 2014-04-16 00:07:33