0

In a php application with I inherited (I am a php newbie) I have a php form with multiple includes. One of the includes has two tables set up and within each table is a form. The first form displays records retrieved from a MySQL database, so there could be 1 or more records returned. A while loop goes through the records and populates the controls with the data for each record (name, phone, email). The controls are named [fieldname<?php echo $line; ?>] - where $line starts at 1 and is incremented as the while loop goes through the records. This is working fine!

The problem comes when someone wants to edit one of the fields and submits the change. The form has an onsubmit="return validateForm(<?php echo $line; ?>);" I have checked to ensure that the $line variable does increment, but when it is sentto the javascript function "validateForm" the variable is not defined in the javasscript function. Originally I did I not pass the $line variable but the javascript function kept telling me that it could not get the value of the undefined element.

FIRST FORM 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 FUNCTION - location in "parent" php form 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;
            }
        } 
3
  • 3
    Do not use mysql functions since they are deprecated. Use mysqli or PDO instead. Commented Oct 29, 2014 at 18:55
  • As far as this javascript function is concerned, pretend for a moment that PHP is not involved. Look directly at the generated HTML to diagnose what's going on. Commented Oct 29, 2014 at 18:56
  • Thanks for the advice TechBrat, unfortunately generated HTML really didn't give me any help whatsoever. Commented Oct 31, 2014 at 22:58

1 Answer 1

0

Your code

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

is outside the loop of linenumbers - so what should $line be, but null?

You either need to move the form into the loop of linenumbers, generating a form per row, or apply the logic on the submit button in question.

If you go with a form per row, you don't even need the linenumber thingy, cause then there would be exactly one value per variable in the submitted array. This would be the preferable way, cause you don't need to submit all values, when you want to modify one row.

And for the validation itself, you also wouldn't need it, cause you could refer to the form in question.

<?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++; }?>
1
  • Thanks dognose, I used part of your solution (put the form in the loop, that helped, but did away with the javascript validation altogether cause it still was not working, did validation on the submit php form instead. Thanks for responding!!! Commented Oct 31, 2014 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.