Tricky Idea I want to snippet up

As you can see, there’s 4 groups of work history information being requested for an application program. What I’m trying to figure out is if the applicant fills out anyone of those areas in From, To, Name/Address/Phone Number, Salary Position, Reason for Leaving the entire group must be filled in and no box can be left empty in that group, if any one of those boxes in the group is empty, then it ignores that part of the form and continues to check the 2nd group, 3rd group, and 4th group for the same thing.

I need advice or someone to help lead by example, Thank you so much in advance!!

Well you could setup a bunch of IF statements looking for empty input but that would be quite a long set…
OR you could setup an array of your input keys e.g. From, To etc and loop through these on processing looking for empty and if found set the GROUP KEY, i.e. (0,1,2,3) to an errors array. You then loop through the group keys an see if they are found in the errors array. IF NOT FOUND, set that key to a completed array.

You then have a condition to check for processing if(!empty($completed)):
You would then use this array to get values.
An example might help.

<?php

if(isset($_POST)):
    $errors = array();
    $fields = array("From","To","Name","Address","Phone","Salary","Position","Reason");
    foreach($fields as $field):
        for($i=0;$i<4;$i++):    
            if(empty($_POST[$field][$i]) && !in_array($i,$errors)):
                $errors[] = $i;
            endif;    
        endfor;
    endforeach;
    
    $completed = array();
    for($i=0;$i<4;$i++):
        if(!in_array($i,$errors)):
            $completed[] = $i;
        endif;
    endfor;
    
    //processing
    if(!empty($completed)):
        foreach($completed as $key):
            
            $values = array();
            foreach($fields as $field):
                $values[] = htmlspecialchars(trim($_POST[$field][$key]));
            endforeach;
            
            //print_r($values);
            $sql = "INSERT INTO table (From, To, Name, Address, Phone, Salary, Position, Reason)
                VALUES(?,?,?,?,?,?,?,?)";
            $stmt = $db->prepare($sql);    
            $stmt->execute($values);
            
        endforeach;
    endif;

endif;

?>
<html>
<head>
<style type="text/css">
.input{float:left; width:45%;}
.fullinput{display: block; float:left; width:90%;}
.label{display: block; float:left; width:45%; text-align:left; font-weight:bold;}
.textinput{float:left;width:90%;}
</style>
<body> 
<form action="" method="post">
<?php
for($i=0;$i<4;$i++):
?>
<div style="float:left;width:25%;">
    <label class="label">From</label>
    <label class="label">To</label>
    <input class="input" type="text" name="From[<?php echo $i; ?>]" value="" />
    <input class="input" type="text" name="To[<?php echo $i; ?>]" value="" />    
    <label class="label">Name</label>
    <label class="label">Phone</label>
    <input class="input" type="text" name="Name[<?php echo $i; ?>]" value="" />    
    <input class="input" type="text" name="Phone[<?php echo $i; ?>]" value="" />    
    <label class="label">Address</label>
    <input class="fullinput" type="text" name="Address[<?php echo $i; ?>]" value="" />    
    <label class="label">Salary</label>
    <label class="label">Position</label>
    <input class="input" type="text" name="Salary[<?php echo $i; ?>]" value="" />    
    <input class="input" type="text" name="Position[<?php echo $i; ?>]" value="" />    
    <label class="label">Reason For Leaving</label>
    <textarea class="textinput" name="Reason[<?php echo $i; ?>]"></textarea>
</div>
<?php
endfor;
?> 
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

oh wow, that seems a bit confusing for an intermediate like me, the way the form goes is that when the form is submitted, it goes thru a separate PHP file that processes the form by emailing an archival email address, the HR Email address, then it sends a copy to the database and then generates a receipt for the applicant. I’m using Parsley for the data validation, and the form processesing goes to a separate php file…just wished things were so simple some how… I’ll take a close look and see how I can modify this extensive code to work with the form…Thanks for your help though!!

Well there is nothing wrong with using a little jquery for validation before POST but I would use a variation of this processing to build and send email and save to DB. This way only the group that has been filled in completely will be sent by email.

The biggest difference in your form and my example is yours is using unique names, e.g.
Employment_From1
Employment_From2 etc.

A modified version of my processing code “could” define these POST keys by adding the number to the name to form a single key, e.g. Employment_From1

As you can see, my form uses ONE group, looped 4 times and the names are arrays, e.g.
From[0]
From[1] etc.

So both in the form and the processing I am running through these array keys 0,1,2 and 3 to define the group and get the value for that input field in the processing section. It’s rather simple really. Also this was just an example page and because I didn’t add any notification code like “Please add Company name”, where it would bounce the user back to the form, there is no reason that the processing couldn’t be on another page.

Anyway, it’s a thought.

ok, I’ll try to figure this out on monday, but thank you for your advice, I’ll let you know how it goes. Thanks again!

ugh, being out of town longer than planned sucks, hahahaha I’ll work on this on wednesday and get back to you Drummin’ :slight_smile:

Can you explain the business logic more?

Could someone fill out 2, 3, and 4 or is it just one of the 4?

If they must fully complete just one of the 4, then maybe adding a radio button next to each one would help?

Or you could have one standard form, where at the top they select which Group they form applies to…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.