提交PHP表单不会将php变量传递给javascript函数

时间:2014-10-29 18:53:18

标签: javascript php forms

在我继承的php应用程序中(我是一个php新手)我有一个包含多个包含的php表单。其中一个包含两个表,每个表中都有一个表单。第一个表单显示从MySQL数据库检索的记录,因此可能会返回1个或多个记录。 while循环遍历记录并使用每条记录(名称,电话,电子邮件)的数据填充控件。控件名为[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 FUNCTION - “父”php表单中的位置  function 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;
            }
        } 

1 个答案:

答案 0 :(得分:0)

您的代码

<form action="admin_profiles_main_update.php" method="post" name="submit_order" enctype="multipart/form-data" onsubmit="return validateForm(<?php echo $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++; }?>