2014-10-29 48 views
0

在我繼承的php應用程序中(我是一個php新手)我有一個包含多個php的表單。其中一個包含兩個表格,每個表格內都有一個表格。第一種形式顯示從MySQL數據庫檢索到的記錄,因此可能會返回1個或多個記錄。 while循環遍歷記錄,並使用每條記錄(名稱,電話,電子郵件)的數據填充控件。該控件的名稱爲[fieldname<?php echo $line; ?>] - 其中$line從1開始,並隨while循環遍歷記錄而遞增。這工作正常!提交PHP表單不會將php變量傳遞給javascript函數

當有人想要編輯其中一個字段並提交更改時,就會出現問題。該表單有一個onsubmit="return validateForm(<?php echo $line; ?>);"我已檢查確保$line變量確實遞增,但是當它發送到javascript函數"validateForm"時,該變量未在javasscript函數中定義。最初我沒有通過$line變量,但javascript函數一直告訴我它無法獲得未定義元素的值。

第一形式CODE

<form action="admin_profiles_main_update.php" method="post" name="submit_order" enctype="multipart/form-data" onsubmit="return validateForm(<?php echo $line; ?>);"> 

     <table border="0" cellspacing="0" cellpadding="0" class="forms"> 
      <col width="20%"/> 
      <col width="80%"/> 
      <?php 
       if($user_access == 'National'){ 
        $result = mysql_query("SELECT * FROM Profile ORDER BY profile_name"); 
       }else{ 
        $result = mysql_query("SELECT * FROM Profile WHERE profile_parent_region = '$user_profile' ORDER BY profile_name"); 

       } 
       $line = 1; 
       while ($row_profile = mysql_fetch_array($result)){ 
        $field_id = "_" . $line; 

        ?> 
        <!-- LINE --> 
        <tr><td colspan="11"><hr class="view_line"></td></tr> 
        <!-- LINE -->   

        <tbody id="region<?php echo $field_id; ?>" > 
         <input class="field_long" name="profile_id<?php echo $field_id; ?>" id="profile_id<?php echo $field_id; ?>" type="hidden" value="<?php echo $row_profile[profile_id]; ?>"/> 

        <tr> 
         <td> 
          <label class="field_label" for="profile_parent_region<?php echo $field_id; ?>">Profile: </label> 
         </td> 
         <td align="left"> 
          <select name="profile_parent_region<?php echo $field_id; ?>" id="profile_parent_region<?php echo $field_id; ?>" class="drop_med" > 
           <option></option> 

           <?php 

           if($user_access != 'National'){ 
             $result_profile = mysql_query("Select Distinct profile_parent_region FROM profile where profile_parent_region = '$user_profile' "); 
           }else { 
             $result_profile = mysql_query("Select Distinct profile_parent_region FROM profile ORDER BY profile_parent_region ASC"); 
           } 
           while($row = mysql_fetch_array($result_profile)){ 
             echo '<option value ="'.$row['profile_parent_region'].'"'; 
             if($row['profile_parent_region'] == $user_profile){ 
              echo ' selected="selected"'; 
             } 
             echo ' > ' . $row['profile_parent_region'] . '</option>'; 
           } 
           ?> 
          </select> 
          <span class="must_fill">* </span> 
          <label class="form_des" for="profile_parent_region<?php echo $field_id; ?>"></label> 
         </td>     
        </tr> 

         <tr> 
         <td> 
          <label class="field_label" for="profile_name<?php echo $field_id; ?>">Region: </label> 
         </td> 
         <td align="left"> 
          <select name="profile_name<?php echo $field_id; ?>" id="profile_name<?php echo $field_id; ?>" class="drop_med" > 
           <option></option> 

           <?php 
           $parent_region = $row_profile['profile_name']; 
           if($user_access != 'National'){ 
             $result_region = mysql_query("Select Distinct region FROM regions where parent_region = '$user_profile' "); 
           }else { 
             $result_region = mysql_query("Select Distinct region FROM regions ORDER BY region ASC"); 
           } 
           while($row = mysql_fetch_array($result_region)){ 
             echo '<option value ="'.$row['region'].'"';           
             if ($row['region'] == $parent_region){ 
              echo ' selected="selected"'; 
             }           
             echo ' >' . $row['region'] . '</option>'; 
           } 
              ?> 
          </select> 
          <span class="must_fill">* </span> 
          <label class="form_des" for="profile_name<?php echo $field_id; ?>"></label> 
         </td>     
        </tr> 
       <tr> 
        <td><label class="field_label" for="profile_manager<?php echo $field_id; ?>" >Region's Manager's Name: </label></td> 
        <td> 
         <input class="field_long" name="profile_manager<?php echo $field_id; ?>" id="profile_manager<?php echo $field_id; ?>" type="input" value="<?php echo $row_profile[profile_manager]; ?>"/> 
         <span class="must_fill">*</span> 
         <label class="form_des" for="profile_manager<?php echo $field_id; ?>"></label> 
        </td>  
       </tr> 

       <tr> 
        <td><label class="field_label" for="profile_phone<?php echo $field_id; ?>" >Region's Contact Number: </label></td> 
        <td> 
         <input class="field_long" name="profile_phone<?php echo $field_id; ?>" id="profile_phone<?php echo $field_id; ?>" type="input" value="<?php echo $row_profile[profile_phone]; ?>"/> 
         <span class="must_fill">*</span> 
         <label class="form_des" for="profile_phone<?php echo $field_id; ?>"></label> 
        </td>  
       </tr> 

       <tr> 
        <td><label class="field_label" for="profile_email<?php echo $field_id; ?>" >Region's Contact E-mail: </label></td> 
        <td> 
         <input class="field_long" name="profile_email<?php echo $field_id; ?>" id="profile_email<?php echo $field_id; ?>" type="input" value="<?php echo $row_profile[profile_email]; ?>"/> 
         <span class="must_fill">*</span> 
         <label class="form_des" for="profile_email">This email address will also be used to advise of files added to a submitted order.</label> 
        </td>  
       </tr> 


      <tr align="center"> 
       <td colspan="2" class="loginrow"> 
        <br /> 
        <input name="Login <?php echo $field_id; ?>" id="Login<?php echo $field_id; ?>" value="Update Profile" type="submit" class="submit_button"/> 
        <br /> 
        <br /> 
       </td> 
      </tr> 


     </tbody>    

     <?php       
        $line++; 
       } 
     ?>  
    </table> 
    </form>` 

Javascript函數 - 位置在 「父」 PHP的形式 功能validateForm(LINENUM){

 if (lineNum == null) { 
      var x = document.forms["submit_order"]["file_name"].value; 
      if (x == null || x == '') { 
       alert("Missing File Name."); 
       return false; 
      } 

      var x = document.forms["submit_order"]["file_for"].value; 
      if (x == null || x == '') { 
       alert("Missing File Type."); 
       return false; 
      } 

      var x = document.forms["submit_order"]["OrderForm"].value; 
      if (x == null || x == '') { 
       alert("Missing File to Upload."); 
       return false; 
      } 
     } 
+3

請勿使用mysql函數,因爲它們已被棄用。改用mysqli或PDO。 – vaso123 2014-10-29 18:55:56

+0

就這個javascript函數而言,假裝暫時不涉及PHP。直接查看生成的HTML來診斷正在發生的事情。 – TecBrat 2014-10-29 18:56:52

+0

感謝TechBrat的建議,不幸的是,生成的HTML確實沒有給我任何幫助。 – 2014-10-31 22:58:24

回答

0

您的代碼

<form action="admin_profiles_main_update.php" method="post" name="submit_order" enctype="multipart/form-data" onsubmit="return validateForm(<?php echo $line; ?>);">

出局ide亞麻的循環 - 那麼$line應該是什麼,但是null

您也需要到窗體進入 linenumbers的循環,產生每排形式,或申請對有問題的提交按鈕的邏輯。

如果你使用每行的表單,你甚至不需要使用linenumber thingy,因爲在提交的數組中每個變量只有一個值。這將是更好的方法,因爲您不需要提交所有值,當您要修改一行時。

而對於驗證本身,您也不需要它,因爲您可以參考有問題的表單。

<?php 
$line = 0; 
while ($row_profile = mysql_fetch_array($result)){ 
    $field_id = "_" . $line; 
    ?> 
    <form action="admin_profiles_main_update.php" method="post" name="submit_order" enctype="multipart/form-data" onsubmit="return validateForm(<?php echo $line; ?>);"> 
    <!-- do all the row stuff --> 
    </form> 
<?$line++; }?> 
+0

感謝dognose,我使用了你的解決方案的一部分(把表單放在循環中,這有助於,但是完全沒有使用javascript驗證,導致它仍然無法工作,而是在提交php表單上進行了驗證。 ! – 2014-10-31 22:56:11