I have a problem with working with array in php what happens, I have the following code:
function AddRows() {
var campos_max = 10;
var x = 0;
$('#add-row-fixe').click(function (e) {
e.preventDefault();
if (x < campos_max) {
$('#new-row').append('<div id="new-row" class="row-f new-row">\
<div class="row-m">\
<div class="row-m">\
<label>\
Tipo de telefone\
</label>\
<input value="Telefone Fixo" type="text" disabled/>\
<input name="tipo_tel[]" value="fixo" type="hidden"/>\
</div>\
<div class="row-m">\
<label>\
DDD\
</label>\
<select name="dd_tel[]" data-number="0">\
<option value="11">11</option>\
</select>\
</div> \
</div>\
<div class="row-m">\
<div class="row-m">\
<label>\
Telefone\
</label>\
<input min="1" name="fone_tel[]" type="number" class="inpt-null"/>\
</div>\
<div class="row-m">\
<label>\
Ramal\
</label>\
<input min="1" max="500" name="ramal_tel[]" type="number" class="inpt-null"/>\
</div>\
</div>\
<a class="btn-danger btn-default remove-row"><span class="lnr lnr-cross"></span> Remover</a>\
</div>\
');
x++;
}
});
$('#add-row-phone').click(function (e) {
e.preventDefault();
if (x < campos_max) {
$('#new-row').append('<div id="new-row" class="row-f new-row">\
<div class="row-m">\
<div class="row-m">\
<label>\
Tipo de telefone\
</label>\
<input value="Telefone Celular" type="text" disabled/>\
<input name="tipo_tel[]" value="celular" type="hidden"/>\
</div>\
<div class="row-m">\
<label>\
DDD\
</label>\
<select name="dd_tel[]" data-number="0">\
<option value="11">11</option>\
</select>\
</div> \
</div>\
<div class="row-m">\
<div class="row-m">\
<label>\
Telefone\
</label>\
<input min="1" name="fone_tel[]" type="number" class="inpt-null"/>\
</div>\
<div class="row-m">\
<label>\
Ramal\
</label>\
<input value="" type="text" disabled/>\
<input name="ramal_tel[' + x + ']" type="hidden"/>\
</div>\
</div>\
<a class="btn-danger btn-default remove-row"><span class="lnr lnr-cross"></span> Remover</a>\
</div>\
');
x++;
}
});
$('#new-row').on("click", ".remove-row", function (e) {
e.preventDefault();
$(this).parent('div').remove();
x--;
});
}
AddRows();
This guy gets me to add and remove new phone lines, landline or cell phone!
Now this is the PHP code that receives and handles this data going ajax:
$data = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$PhoneArr = array();
$PhoneArr['tipo_tel'] = $data['tipo_tel'];
$PhoneArr['dd_tel'] = $data['dd_tel'];
$PhoneArr['fone_tel'] = $data['fone_tel'];
$PhoneArr['ramal_tel'] = $data['ramal_tel'];
$dataPhone = array();
$dataPhoneCount = count($PhoneArr['tipo_tel']);
$dataPhoneKeys = array_keys($PhoneArr);
for ($i = 0; $i < $dataPhoneCount; $i++):
foreach ($dataPhoneKeys as $Keys):
$dataPhone[$i][$Keys] = $PhoneArr[$Keys][$i];
endforeach;
endfor;
foreach ($dataPhone as $dat):
extract($dat);
if ($tipo_tel === 'celular'):
if (!preg_match('/^[0-9]{9}$/', $fone_tel)):
echo 'Verifique o telefone celular ' . $fone_tel;
endif;
endif;
if ($tipo_tel === 'fixo'):
if (!preg_match('/^[0-9]{8}$/', $fone_tel)):
echo 'Verifique o telefone comercial ' . $fone_tel;
endif;
endif;
var_dump($dat);
endforeach;
When I do a submit the first time for example on 3 phone lines it returns me or error, but when I click the remove button for example the 1 line, it returns me this error Undefined offset: 1
and the line number is just this guy here: $dataPhone[$i][$Keys] = $PhoneArr[$Keys][$i];
.
Anybody here? Help?