2011-11-08 27 views
0

我在PHP完整newbee(開始就在上週)PHP形式(價值消失,不知道它是如何被處理)

的問題是這樣的:

基本上,我試圖確保填寫子表格後,不會更改。因此,我用!isset來顯示子表單(即如果!isset爲真),如果!isset爲假,那麼它隱藏該子表單並顯示下一個子表單(個體表單只是被設計的) 。

 <?php include($_SERVER['DOCUMENT_ROOT'].'/officespace/includes/functions.php'); 

    echo'<html> 
     <head> 
      <title> Create </title> 
     </head> 

    <body>'; 

    if(!isset($_POST["Category"])){ 
    /* if no category is selected, then this code will display the form to select the category*/ 

    Echo "Pls Select Category before clicking on Submit Category"; 
    /* Breaking out of PHP here, to make the form sticky by using a php code inside form action*/ 
    ?> 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    <fieldset> 
     <legend>Select the Category of Person: </legend><br /> 
      <input type="radio" name="Category" value="Individual" /> Individual<br /><br /> 
      <input type="radio" name="Category" value="Company, Pvt Ltd" /> Company, Pvt Ltd<br /><br /> 
      <input type="radio" name="Category" value="Company, Ltd" /> Company, Ltd<br /><br /> 
      <input type="radio" name="Category" value="Partnership Firm" /> Partnership Firm<br /><br /> 
      <input type="radio" name="Category" value="LLP Firm" /> LLP Firm<br /><br /> 
      <input type="radio" name="Category" value="HUF" /> HUF<br /><br /> 
      <input type="submit" name='Submit Category' value="Submit Category" /><br /> 
    </fieldset> 
    </form> 
    <?php 
    } Else { 

    $Category = $_POST["Category"]; 


     Echo "$Category"; 
     Echo "<br />"; 
    /* Using swich statement to test the value of Category, and accordingly echo appropriate forms*/ 
     switch ($Category) { 
      case "Individual": 
      if(!isset($_POST['Submit_Data_for_Individual'])){ 

      //if no data for individual is submitted, 
      //then this code will display the form to enter and submit data for Individual 

       Echo /*displays message*/ 
       "Pls Enter the Data for the Individual"; 
    ?> 
       <form action="<?php Echo $_SERVER['PHP_SELF']; ?>" method="post"> 
       <fieldset> 
       <br /> 
        First Namee: <input type="text" name="Individual_First_Name" /> 
       <br /> 
        Middle Name: <input type="text" name="Individual_Middle_Name" /> 
       <br /> 
        Last Name: <input type="text" name="Individual_Last_Name" /> 
       <br /> 
        Date of Birth: <input type="text" name="date_of_birth/incorporation" /> 
       <br /> 
        Gender: 
       <br /> 
        <input type="radio" name="Gender" value="male" /> Male 
       <br /> 
        <input type="radio" name="Gender" value="female" /> Female 
       <br /> 
        Email 1: <input type="text" name="email_1" /> 
       <br /> 
       <input type="submit" name="Submit_Data_for_Individual" value="Submit Data for Individual" /> 
       </fieldset> 
       </form> 
    <?php        
       }Else 
       { 
       $email_1 = $_POST["email_1"]; 
       $Gender = $_POST["Gender"]; 

       validate_email($email_1); // this is a custom function which i made 
       Echo $Gender; // just to see if value has been passes. problem lies here because its not showing anything 

       // run other validations here 
       // and if all valid then run mysqli insert query for individuals record 
       } 

       break; 
      case "Company, Pvt Ltd": 
       echo "Company, Pvt Ltd"; 
       break; 
      case "Company, Ltd": 
       echo "Company, Ltd"; 
       break; 
      case "Company, Ltd": 
       echo "Company, Ltd"; 
       break; 
      case "Partnership Firm": 
       echo "Partnership Firm"; 
       break; 
      case "LLP Firm": 
       echo "LLP Firm"; 
       break; 
      case "HUF": 
       echo "HUF"; 
       break; 
      case NULL: 
       echo "Error: nothing selected"; 
       break; 

     } 
    } 

    echo '</body> 


    </html>'; 

    ?> 

回答

3

立即看到一個問題。

您正在檢查一個名爲Submit Data for Individual的表單輸入,但這是一個提交按鈕的value,它沒有name屬性。設置上的提交按鈕name='submit-data'屬性和更改條件檢查的名稱,而不是它的值:

// This will never match. 
if(!isset($_POST["Submit Data for Individual"])){ 

// Change it to 
if(!isset($_POST["submit-data"])){ 

// Then change this 
<input type="submit" value="Submit Data for Individual" /> 

// To this: 
<input type="submit" name='submit-data' value="Submit Data for Individual" /> 

此外,在switch語句的默認情況下使用default關鍵字:

// You may safely change this: 
    case NULL: 
     echo "Error: nothing selected"; 
     break; 

    // To this: 
    default: 
     echo "Error: nothing selected"; 
     break; 

附錄

下面的代碼是永遠無法到達,因爲形式的帖子轉到其他腳本,create.php。如果將<form>操作屬性更改爲<?php $_SERVER['PHP_SELF'];?>而不是create.php,則應該看到else大小寫。現在,它不起作用,因爲您的if測試$_POST["submit-data"]已設置。只有在表單已提交時才能設置,但表單會提交給外部腳本。

 // This else case can never be reached... 
     }Else 
     { 
     validate_email($_POST["email_1"]); // this is a custom function which i made 
     Echo $_POST["Gender"]; // just to see if value has been passes. problem lies here because its not showing anything 

     // run other validations here 
     // and if all valid then run mysqli insert query for individuals record 
     } 

要解決這個問題,看看你的Gender呼應出來,臨時更改

<form action="create.php" method="post"> 
// change to 
<form action="' . $_SERVER['PHP_SELF'] . '" method="post"> 

附錄2

如果分類設置要檢查,但張貼該使用者的形式之後,將不會是:

// Change 
if(!isset($_POST["Category"])){ 

// To check that the user form was not submitted 
if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) { 

然後,您需要測試用戶表單是否已提交。在Else { $Category = $_POST['Category'];部分之前,添加一個else if來處理用戶表單。

if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) { 
    // Show the Category form... 
} 
// Process the user form... 
else if (isset($_POST['submit-data'])) { 
    validate_email($_POST["email_1"]); // this is a custom function which i made 
    Echo $_POST["Gender"]; 
} 
// Now process the categories or show the user form... 
else { 
    $Category = $_POST['Category']; 
    // etc... 
} 

最後,從individual情況下刪除整個Else塊,因爲它不能在那裏使用。

+0

謝謝先生..我很抱歉,我沒有經驗的HTML(僅今天開始表格) –

+1

@ChinmayKamat我們都開始在某個地方。 –

+0

我很抱歉地說,但它仍然不工作。 –