2013-02-05 36 views
0

我無法解決代碼中的邏輯錯誤。我不知道什麼是錯的,雖然它似乎邏輯是正確的PHP表單代碼中的PHP邏輯錯誤

這是我的PHP:

<?php require_once("includes/connection.php"); ?> 
<?php 
include_once("includes/form_functions.php"); 
if(isset($_POST['submit'])) 
{ 
    $errors = array(); 
    if(isset($_POST['txtSpace'])) 
    { 
     $choice_spc_port = $_POST["txtSpace"]; 
    } 
    if(isset($_POST['txtNumber'])) 
    { 
     $choice_no = $_POST["txtNumber"]; 
    } 
    if(isset($_POST['txtLocation'])) 
    { 
     $choice_loc = $_POST["txtLocation"]; 
     if($choice_loc =="txtSetXY") 
     { 
      $x = $_POST["txtXLocation"]; 
      $y = $_POST["txtYLocation"]; 
      if($x == "") 
      { 
       $message = "You forgot to enter X Value"; 
      } 
      elseif($y == "") 
      { 
       $message = "You forgot to enter Y Value"; 
      } 
      else 
      { 
       $choice_loc = $x . "," . $y; 
      } 
     } 
    } 
    $user_name = $_POST["txtUserName"]; 
    $user_email = $_POST["txtUserEMail"]; 
    $animal_name = $_POST["txtAnimalName"]; 
    $disp_msg = $_POST["txtDispMsg"]; 
    $comments = $_POST["txtComments"]; 
    if(!isset($_POST['txtSpace'])) 
    { 
     $message = "Please select Space Portion"; 
    } 
    elseif(!isset($_POST['txtNumber'])) 
    { 
     $message = "Please select the number of animals"; 
    } 
    elseif(!isset($_POST['txtLocation'])) 
    { 
     $message = "Please select the desired location of animal"; 
    } 
    elseif($user_name == "") 
    { 
     $message = "Please enter your name."; 
    } 
    elseif($user_email == "") 
    { 
     $message = "Please enter your email."; 
    } 
    elseif($animal_name == "") 
    { 
     $message = "Please enter the name of the animal."; 
    } 
    elseif($disp_msg == "") 
    { 
     $message = "What message you want to dedicate to the animal?."; 
    } 
    else 
    { 
     // validation 
     $required_fields = array('txtUserName','txtUserEMail','txtAnimalName','txtDispMsg'); 
     $errors = array_merge($errors, check_required_fields($required_fields, $_POST)); 
     $user_name = trim(mysql_prep($_POST['txtUserName'])); 
     $user_email = trim(mysql_prep($_POST['txtUserEMail'])); 
     $animal_name = trim(mysql_prep($_POST['txtAnimalName'])); 
     $disp_msg = trim(mysql_prep($_POST['txtDispMsg'])); 

     if(empty($errors)) 
     { 
      /*if($choice_loc == "txtSetXY") 
      { 
       $x = $_POST["txtXLocation"]; 
       $y = $_POST["txtYLocation"]; 
       $choice_loc = $x . "," . $y; 
      }*/ 
      if($choice_no == "other") 
      { 
       $choice_no = $_POST["other_field"]; 
      } 

      $insert = "INSERT INTO db_form (db_space_portion, db_number, db_location, db_user_name, db_user_email, db_animal_name, db_message, db_comments) VALUES ('{$choice_spc_port}', '{$choice_no}', '{$choice_loc}', '{$user_name}', '{$user_email}','{$animal_name}','{$disp_msg}','{$comments}')"; 
      $result = mysql_query($insert); 
      if($result) 
      { 
       echo("<br>Input data is succeed"); 
      } 
      else 
      { 
       $message = "The data cannot be inserted."; 
       $message .= "<br />" . mysql_error(); 
      } 
     } 
     else 
     { 
      if(count($errors) == 1) 
      { 
       $message = "There was 1 error on the form."; 
      } 
      else 
      { 
       $message = "There were " . count($errors) ." errors on the form."; 
      } 
     } 
    } 
} 
else 
{ 
    $user_name = ""; 
    $user_email = ""; 
    $disp_msg = ""; 
    $comments = ""; 
} 
    ?> 
    <!DOCTYPE html> 
    <html lang="en"> 
    <head> 
    <title>Test Form</title> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="css/reset.css" type="text/css" media="all"> 
    <link rel="stylesheet" href="css/layout.css" type="text/css" media="all"> 
    <link rel="stylesheet" href="css/style.css" type="text/css" media="all"> 
    <script type="text/javascript" src="js/jquery-1.9.0.min.js" ></script> 
    <script type="text/javascript" src="js/cufon-yui.js"></script> 
    <script type="text/javascript" src="js/cufon-replace.js"></script> 
    <script type="text/javascript" src="js/Copse_400.font.js"></script> 
    <script type="text/javascript" src="js/imagepreloader.js"></script> 
    <script type="text/javascript" src="js/functions.js"></script> 
    <!--[if lt IE 9]> 
    <script type="text/javascript" src="js/ie6_script_other.js"></script> 
     <script type="text/javascript" src="js/html5.js"></script> 
    <![endif]--> 
     </head> 
     <body id="page5"> 
     <!-- START PAGE SOURCE --> 

     <div class="body7"> 
     <div class="main"> 
     <section id="content"> 
     <div class="wrapper"> 
     <article class="col24"> 
     <div class="pad1"> 
     <h4>Kindly Fill the form</h4> 
     <?php if(!empty($message)){ echo $message; } ?> 
     <?php if(!empty($errors)){ echo display_errors($errors);}?> 
     <form id="TestForm" name="TestForm" method="post" action="form.php"> 
      <div> 

      <div class="wrapper"> <strong><span>*</span> Desired Space</strong> 
      <div class="formText"> 
       <input type="radio" name="txtSpace" value="RJ"/>Space Top<br /> 
       <input type="radio" name="txtSpace" value="SM" />Space Bottom<br /> 
       </div> 
      </div> 

      <div class="wrapper"> <strong><span>*</span> Select the Number</strong> 
      <div class="formText"> 
       <input type="radio" name="txtNumber" value="100"/>100 
       <input type="radio" name="txtNumber" value="200"/>200 
       <input type="radio" name="txtNumber" value="500"/>500 
       <input type="radio" name="txtNumber" value="1000"/>1000 
       <input type="radio" name="txtNumber" value="10000"/>10000 
       <input type="radio" name="txtNumber" value="other"/>other 
       <input type="text" name="other_field" id="other_field" onblur="checktext(this);"/> 
      </div> 
      </div> 

      <div class="wrapper"> <strong><span>*</span> Select X & Y Value</strong> 
       <div class="formText"> 
       <input type="radio" name="txtLocation" value="txtSetXY"/> Specify Photo Location<br /> 
        <div style="padding-left:20px;"> 
        X: <input type="text" id="locField" name="txtXLocation"><br /> 
        Y: <input type="text" id="locField" name="txtYLocation"><br /> 
        </div> 
        <input type="radio" name="txtLocation" value="Default"/>Default 
       </div> 

      </div> 


      <div class="wrapper"> <strong><span>*</span> Your Name:</strong> 
       <div class="bg"> 
       <input type="text" class="input" name="txtUserName"> 
       </div> 
      </div> 
      <div class="wrapper"> <strong><span>*</span> Your Email:</strong> 
       <div class="bg"> 
       <input type="text" class="input" name="txtUserEMail"> 
       </div> 
      </div> 
      <div class="wrapper"> <strong><span>*</span> Name of the animal:</strong> 
       <div class="bg"> 
       <input type="text" class="input" name="txtAnimalName"> 
       </div> 
      </div> 

      <div class="wrapper"> 
      <div class="textarea_box"> <strong><span>*</span> The Message you want for your favourite animal:</strong> 
       <textarea name="txtDispMsg" cols="1" rows="1"></textarea> 
      </div> 
      </div> 

      <div class="wrapper"> 
      <div class="textarea_box"> <strong>Comments:</strong> 
       <textarea name="txtComments" cols="1" rows="1"></textarea> 
      </div> 
      </div> 

      <input type="submit" name="submit" value="Submit"> 
      </div> 
     </form> 
     </div> 
    </article> 
    </div> 
</section> 
</div> 
</div> 
</body> 
</html> 

錯誤:

檢查這個PHP小提琴這裏。

line 25.即使我將x textfield留爲空白,也不會顯示此消息 $ message =「您忘記輸入X值了」;

與第29行相同。即使我將y textfield留爲空白,也不會顯示此消息 $ message =「您忘記輸入Y值了」;

但是,如果我在x和y textfield中輸入值,即在txtXLocation和txtYLocation中輸入值,則它們將保存在db中,這意味着它只是不檢查驗證。

在此先感謝

+0

嘗試使用'if(empty($ x))' – Kamil

+1

歡迎使用Stack Overflow。請注意,您可以(實際上鼓勵您)在問題本身發佈代碼。只需使用** {} **工具欄按鈕進行格式化即可。 –

+0

@Kamil我使用相同的..但它不提供解決方案 – Sumit

回答

0

請確保您有connection.php文件中包含文件夾,你給了正確的路徑到達該文件。

+0

這不是錯誤..所有的路徑是正確的。 ine 25.即使我離開x textfield空白$ message =「你忘了輸入X值」,這從來沒有顯示。 與第29行相同。即使將textfield留空,也不會顯示此消息$ message =「您忘記輸入Y值」; 但是,如果我在x和y textfield中輸入值,即在txtXLocation和txtYLocation中輸入值,則它們將保存在db中,這意味着它只是不檢查驗證。 – Sumit

+0

我不能看到$消息推入$ errors數組。當有錯誤時,您覆蓋$消息,可能會有問題。把$ message放入$ errors數組中 –