簡體   English   中英

提交PHP表單不會將php變量傳遞給javascript函數

[英]Submit PHP form does not pass php variable to javascript function

在我繼承的php應用程序中(我是php新手),我有一個包含多個include的php表單。 其中一個包含兩個表,每個表中都有一個表單。 第一種形式顯示從MySQL數據庫檢索的記錄,因此可能返回1條或更多條記錄。 while循環遍歷記錄,並使用每個記錄(名稱,電話,電子郵件)的數據填充控件。 控件被命名為[fieldname<?php echo $line; ?>] [fieldname<?php echo $line; ?>] -其中$line從1開始,並隨着while循環遍歷記錄而增加。 一切正常!

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

第一表格代碼

<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;
            }
        } 

您的密碼

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

$line循環之外 -因此$line應該是什么,但是為null

您要么需要將表單移到行號循環中, 每行生成一個表單,要么將邏輯應用於有問題的“提交”按鈕。

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

對於驗證本身,您也將不需要它,因為您可以引用有問題的表單。

<?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++; }?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM