2

I have post ajax data with all data from form. Users can add dynamically blocks with rows for previous employers. [empName] maybe 10 or 20 ... or 2 ...How to catch count of previous employers have and save it to variable? Here is post data:

["step-3"]=> string(1065) "{
"empYrXp":"min10yexp",
"empName-1":"TEST PAYPAL TEST",
"empStr-1":"TEST ORDER PAYPAL",
"empCity-1":"Pleven",
"empState-1":"CT",
"empContactPerson-1":"mbnmbnmbnm",
"empContactPersonPhone-1":"999-999-9999",
"empFromDate-1":"08-2013",
"empToDate-1":"06-2019",
"empPositionHeld-1":"bnmb",
"empReasonForLeaving-1":"nmbnmbnmbnm",
"empWereSubject-1":"No",
"empWasPrevJobDesignated-1":"No",
"empName-2":"TEST PAYPAL TEST",
"empStr-2":"TEST ORDER PAYPAL",
"empCity-2":"Pleven",
"empState-2":"CA",
"empContactPerson-2":"fghfhfhfgh",
"empContactPersonPhone-2":"999-999-9999",
"empFromDate-2":"02-2008",
"empToDate-2":"08-2013",
"empPositionHeld-2":"fghfgh",
"empReasonForLeaving-2":"fghfghfgh",
"empWereSubject-2":"No",
"empWasPrevJobDesignated-2":"No",
"empName-3":"TEST TEST",
"empStr-3":"TEST ORDER PAYPAL",
"empCity-3":"Pleven",
"empState-3":"CA",
"empContactPerson-3":"sdfsdfsdfsdf",
"empContactPersonPhone-3":"999-999-9999",
"empFromDate-3":"01-2006",
"empToDate-3":"01-2006",
"empPositionHeld-3":"sdfsdfs",
"empReasonForLeaving-3":"dfsdfsdfsdf",
"empWereSubject-3":"No",
"empWasPrevJobDesignated-3":"No"
}" 

i try to count prevouse employer like this

$countAllEmp = ( count($jsonDecode3) - 1 ) / 12;

but when i try save data, the endless rotation of the query ... here is what i am tried:

for($b = 1; $b <= $countAllEmp; $b++){

if(isset($jsonDecode3["empName-".$b.""]) && $jsonDecode3["empName-".$b.""]!=''){
$empName.$b = $jsonDecode3["empName-".$b.""];
}else{
$empName.$b = '';
}
if(isset($jsonDecode3["empStr-".$b.""]) && $jsonDecode3["empStr-".$b.""]!=''){
$empStr.$b = $jsonDecode3["empStr-".$b.""];
}else{
$empStr.$b = '';
}
if(isset($jsonDecode3["empCity-".$b.""]) && $jsonDecode3["empCity-".$b.""]!=''){
$empCity.$b = $jsonDecode3["empCity-".$b.""];
}else{
$empCity.$b = '';
}
if(isset($jsonDecode3["empState-".$b.""]) && $jsonDecode3["empState-".$b.""]!=''){
$empState.$b = $jsonDecode3["empState-".$b.""];
}else{
$empState.$b = '';
}
if(isset($jsonDecode3["empContactPerson-".$b.""]) && $jsonDecode3["empContactPerson-".$b.""]!=''){
$empContactPerson.$b = $jsonDecode3["empContactPerson-".$b.""];
}else{
$empContactPerson.$b = '';
}
if(isset($jsonDecode3["empContactPersonPhone-".$b.""]) && $jsonDecode3["empContactPersonPhone-".$b.""]!=''){
$empContactPersonPhone.$b = $jsonDecode3["empContactPersonPhone-".$b.""];
}else{
$empContactPersonPhone.$b = '';
}
if(isset($jsonDecode3["empFromDate-".$b.""]) && $jsonDecode3["empFromDate-".$b.""]!=''){
$empFromDate.$b = $jsonDecode3["empFromDate-".$b.""];
}else{
$empFromDate.$b = '';
}
if(isset($jsonDecode3["empToDate-".$b.""]) && $jsonDecode3["empToDate-".$b.""]!=''){
$empToDate.$b = $jsonDecode3["empToDate-".$b.""];}else{$empToDate.$b = '';
}
if(isset($jsonDecode3["empPositionHeld-".$b.""]) && $jsonDecode3["empPositionHeld-".$b.""]!=''){
$empPositionHeld.$b = $jsonDecode3["empPositionHeld-".$b.""];
}else{
$empPositionHeld.$b = '';
}
if(isset($jsonDecode3["empReasonForLeaving-".$b.""]) && $jsonDecode3["empReasonForLeaving-".$b.""]!=''){
$empReasonForLeaving.$b = $jsonDecode3["empReasonForLeaving-".$b.""];
}else{
$empReasonForLeaving.$b = '';
}
if(isset($jsonDecode3["empWereSubject-".$b.""]) && $jsonDecode3["empWereSubject-".$b.""]!=''){
$empWereSubject.$b = $jsonDecode3["empWereSubject-".$b.""];
}else{
$empWereSubject1.$b = '';
}
if(isset($jsonDecode3["empWasPrevJobDesignated-".$b.""]) && $jsonDecode3["empWasPrevJobDesignated-".$b.""]!=''){
$empWasPrevJobDesignated.$b = $jsonDecode3["empWasPrevJobDesignated-".$b.""];
}else{
$empWasPrevJobDesignated.$b = '';
}
}

this is the button for add new box with all inputs:

function addEmployers(years) {
var $ = jQuery,
    html = '<div class="employers"> <div class="employer"> <p class="text-center">Employer #1 : (the most recent)</p><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Name: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control" id="empName-1" name="empName-1" data-validation="length" data-validation-length="1-100"> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Street: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control" id="empStr-1" name="empStr-1" data-validation="letternumeric" data-validation-allowing="-,.\'` "> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>City: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control" id="empCity-1" name="empCity-1" data-validation="length" data-validation-length="1-100"> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>State: *</label> </div><div class="col-sm-6 form-group"> <select class="form-control" id="empState-1" name="empState-1" data-validation="length" data-validation-length="min1">' + genStates() + '</select> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Contact person: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control" id="empContactPerson-1" name="empContactPerson-1" data-validation="length" data-validation-length="1-100"> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Phone number: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control phone-mask" id="empContactPersonPhone-1" placeholder="123-456-7890" name="empContactPersonPhone-1" data-validation="custom" data-validation-regexp="^([0-9]{3}\-[0-9]{3}\-[0-9]{4})$"> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Date from: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control datepicker-no-day" id="empFromDate-1" name="empFromDate-1" data-validation="' + years + '" data-validation-format="mm-yyyy" readonly> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Date to: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control datepicker-no-day" id="empToDate-1" name="empToDate-1" data-validation="' + years + '" data-validation-format="mm-yyyy" readonly> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Position held: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control" id="empPositionHeld-1" name="empPositionHeld-1" data-validation="length" data-validation-length="1-100"> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Reason for leaving: *</label> </div><div class="col-sm-6 form-group"> <input type="text" class="form-control" id="empReasonForLeaving-1" name="empReasonForLeaving-1" data-validation="length" data-validation-length="1-100"> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Were you subject to the Federal Motor Carrier Safety Regulations (FMCSRs) while employed by the previous employer? *</label> </div><div class="col-sm-6 form-group"> <label class="radio-inline"> <input type="radio" name="empWereSubject-1" value="Yes" data-validation="required"> Yes </label> <label class="radio-inline"> <input type="radio" name="empWereSubject-1" value="No" data-validation="required"> No </label> </div></div><div class="row"> <div class="col-sm-5 col-sm-offset-1 text-right"> <label>Was the previous job position designated as a safety sensitive function in any DOT regulated mode, subject to alcohol and controlled substances testing requirements as required by 49 CFR Part 40? *</label> </div><div class="col-sm-6 form-group"> <label class="radio-inline"> <input type="radio" name="empWasPrevJobDesignated-1" value="Yes" data-validation="required"> Yes </label> <label class="radio-inline"> <input type="radio" name="empWasPrevJobDesignated-1" value="No" data-validation="required"> No </label> </div></div></div><div class="text-center"> <button class="text-center btn btn-danger add-prev-emp" onclick="addPrevEmp(\'' + years + '\')" disabled>Add previous employer</button> </div></div>';

$(html).insertAfter($('.empAfterHere'));
setTimeout(function () {
    setupValidate();
    $('.datepicker-no-day').datetimepicker({
        format: 'MM-YYYY',
        ignoreReadonly: true,
        maxDate: moment()
    });
    $('.datepicker-no-day').on('dp.change', function (e) {
        var target = $(e.currentTarget);
        var id = target.attr('id');
        var idNum = parseInt(id.substring(id.length - 1));
        if (target.is('[id^="empFromDate"]')) {
            $('#empToDate-' + idNum).data('DateTimePicker').minDate(e.date);
            if ($('#empFromDate-' + (idNum + 1)).length > 0) {
                $('#empFromDate-' + (idNum + 1)).data('DateTimePicker').maxDate(e.date);
                $('#empToDate-' + (idNum + 1)).data('DateTimePicker').maxDate(e.date);
            }
        }
    });

    // Mask some fields
    $('.phone-mask').mask('999-999-9999');
}, 500);

}

6
  • Can you edit your php code, so that it is readable for all of us? The likelihood that your problem will be solved by reasonably readable code is then higher. Did you debug $countAllEmp? What 's the value of this variable? Commented Jun 27, 2019 at 13:40
  • ok, i will edit it ... the value in this case is 3 Commented Jun 27, 2019 at 13:52
  • Do you have control over the form? It will be easier to rebuild the form. Commented Jun 27, 2019 at 14:06
  • yes, i can rebiult it ... with different names of inputs? Commented Jun 27, 2019 at 14:08
  • Yep. With different names. Commented Jun 27, 2019 at 14:10

1 Answer 1

1

Don't know how you get your json, but forms support naming with [] which results in arrays on server side:

<input name="someName[]" />
<input name="someName[]" />
<input name="someName[]" />

Try outputting $_POST['someName'], it will be 0-indexed array with 3 elements.

Moreover, you can define indexes in [] explicitly:

<input name="someName[11]" />
<input name="someName[12]" />
<input name="someName[13]" />

Try outputting $_POST['someName'], it will be array with 3 keys - 11, 12, 13.

So, you have to rename you fields instead of empName-, empStr- etc. - use [] notation: empName[], empStr[] etc.

Sign up to request clarification or add additional context in comments.

Comments

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.