Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table name education qualification in my form. i want validation on qualification table. they have total 5 fields out of which user decide to fill. but i need validation on, if a user enter exam passed then he will have to add all details of exam passed.

for e.g
row exam passed year marks percentage
1 10 200 170 60
2 12
3
4
5

in this table there are 5 rows but user have to decide how many columns are going to fill... but if user enter in empty row like "12" so he need to enter year marks and percentage.

PHP
foreach ($post['exam_passed'] as $id => $slno) {
    if (empty($exam_passed_))  {
      $error[] = 'exam_passed_' . $id;
      $error_message[] = 'exam_passed_ can not be blank.';
    }
  }


foreach ($post['univ'] as $id => $slno) {
    if (empty($univ_)) {
      $error[] = 'univ_' . $id;
      $error_message[] = 'univ_ can not be blank.';
    }
  }

foreach ($post['years_passing'] as $id => $slno) {
    if (empty($years_passing_)) {
      $error[] = 'years_passing_' . $id;
      $error_message[] = 'years_passing_ can not be blank.';
    }
  }

foreach ($post['attempt'] as $id => $slno) {
    if (empty($attempt_)) {
      $error[] = 'attempt_' . $id;
      $error_message[] = 'attempt_ can not be blank.';
    }
  }

foreach ($post['max_marks'] as $id => $slno) {
    if (empty($max_marks_)) {
      $error[] = 'max_marks_' . $id;
      $error_message[] = 'max_marks_ can not be blank.';
    }
  }


<pre lang="xml"><li> Academic Qualification:(Examinations passed from Metic/Higher Secondary onwards to Doctorate/Research degrees).
                        <table cellpadding="5px" border="1">
                          <thead>
                            <tr>
                              <td>
                              <center>Examination</center>
                              </td>
                              <td>
                              <center>Name of the Univ./Board</center>
                              </td>
                <td>
                              <center>Year of Passing</center>
                              </td>
                <td>
                              <center>Attempts in which passed</center>
                              </td>
                              <td>
                              <center>Max. Marks</center>
                              </td>

                              <td>
                              <center>Marks Obtained</center>
                              </td>
                              <td>
                              <center>Percentage</center>
                              </td>
                <td>
                              <center>Hons Distinction(Position)</center>
                              </td>
                            </tr>
                          </thead>
                          <tbody>
                           <?php for ($i = 0; $i<13; $i++): ?>
                            <tr>

                              <td>
<div <?php (in_array('exam_passed_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="exam_passed[]" value="<?php !empty($post['exam_passed'][$i]) ? print $post['exam_passed'][$i] : '';?>"/></td>
                              <td>
<div <?php (in_array('univ_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="univ[]" value="<?php !empty($post['univ'][$i]) ? print $post['univ'][$i] : '';?>"/></td>

                              <td>
<div <?php (in_array('years_of_passing_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="years_passing[]" value="<?php !empty($post['years_passing'][$i]) ? print $post['years_passing'][$i] : '';?>"/></td>
                <td>
<div <?php (in_array('attempt_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="attempt[]" value="<?php !empty($post['attempt'][$i]) ? print $post['attempt'][$i] : '';?>"/></td>
                <td>
<div <?php (in_array('max_marks_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="max_marks[]" value="<?php !empty($post['max_marks'][$i]) ? print $post['max_marks'][$i] : '';?>"/></td>
                <td>
<div <?php (in_array('marks_obt_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="marks_obt[]" value="<?php !empty($post['marks_obt'][$i]) ? print $post['marks_obt'][$i] : '';?>"/></td>
                              <td>
<div <?php (in_array('percentage_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="percentage[]" value="<?php !empty($post['percentage'][$i]) ? print $post['percentage'][$i] : '';?>"/></td>
                              <td>
<div <?php (in_array('position_' . $i, $error) ? print "class='error'" : '') ?>><input type="text" name="position[]" value="<?php !empty($post['position'][$i]) ? print $post['position'][$i] : '';?>"/></td>
                            </tr>
                            <?php endfor; ?>

                          </tbody>
                        </table>
                        </li>
Posted
Updated 10-Mar-20 3:06am

1 solution

Your very first code shows you have to get an understanding of just what it is you are doing - if this is the form target then what is the source of $post?

If it's supposed to be the form it would be $_POST and it would NOT be an array:
foreach ($post['exam_passed'] as $id => $slno) {
    if (empty($exam_passed_))  {
      $error[] = 'exam_passed_' . $id;
      $error_message[] = 'exam_passed_ can not be blank.';
    }
  }


You have a foreach loop for $post (from whatever source you get it from) would thus be an array operation on a value that is not an array, but a member of one.

It's typically a better idea to validate on the client side (unless data needs to be checked on the server) - which is done with javaScript.

It just seems to big a mess to fix until you get it all in some logical order.
 
Share this answer
 
v2
Comments
Richard Deeming 10-Mar-20 9:11am    
Check the date on the question - I'd hope the OP isn't still waiting for an answer. :)
W Balboos, GHB 10-Mar-20 9:14am    
Someone had to do something to push it up towards the top of the list - although in this case I think I agree with you. Well - at least now it's off the list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900