I have a form, where user can insert up to 4 age ranges. User can add or remove input fields dynamically.
I am trying to send the array to my PHP file and then insert it to database (insert new row for each range pair, lets say 1-3 and 4-6 will be on separate rows with correct user_id) with PDO.
The callback is success in PHP file, but nothing is inserted, which means it cannot get the values from JS and I am pretty lost how to send the array with the rest of the form data.
In html I have a button where, user can add fields dynamically and the ID will increment by 1 each time.
HTML file
<div class="row default" id="childAgeRange1">
<div class="col-md-6">
<input type="text" class="form-control" id="userProfileAgeFrom" name="userProfileAgeFrom[]" placeholder="Alates..." />
</div>
<div class="col-md-6">
<input type="text" class="form-control" id="userProfileAgeTo" name="userProfileAgeTo[]" placeholder="Kuni..." />
</div>
</div>
My JS file:
$(document).on("click", "#btnUpdateInformation", function (e) {
var userProfileAgeFrom = $("input[name^='userProfileAgeFrom']").map(function (idx, ele) {
return $(ele).val();
}).get();
var userProfileAgeTo = $("input[name^='userProfileAgeTo']").map(function (idx, ele) {
return $(ele).val();
}).get();
var formData = {
'user_id' : $("#hiddenUserID").val(),
'userPassword' : $('#userPassword').val(),
'userRetypePassword' : $('#userRetypePassword').val(),
'userBirthCountry' : $('#userBirthCountry').val(),
'userBirthCity' : $('#userBirthCity').val(),
'userBirthAddress' : $('#userBirthAddress').val(),
'UserZipCode' : $('#UserZipCode').val(),
'userFirstName' : $('#userFirstName').val(),
'userLastName' : $('#userLastName').val(),
'userSex' : $('#userSex').val(),
'userBirthDay' : $('#userBirthDay').val(),
'userBirthMonth' : $('#userBirthMonth').val(),
'userBirthYear' : $('#userBirthYear').val(),
'userPhoneNr' : $('#userPhoneNr').val(),
'userPasswordConfirm' : $('#userPasswordConfirm').val(),
'userQuote' : $('#userQuote').val(),
'userDescription' : $('#userDescription').val(),
'userProfileAgeFrom' : userProfileAgeFrom,
'userProfileAgeTo' : userProfileAgeTo,
'userProfileWage' : userFinalWage
};
console.log(formData);
$.ajax({
type: "POST",
url: "PHP/updateUserProfile.php",
data: formData,
success: function(data){
console.log(data);
if(data.status == 'success'){
console.log("success");
}else if(data.status == 'error'){
console.log("error");
}else if(data.status == 'no_results'){
console.log("no results");
}else if(data.status == 'results'){
console.log("there are results");
}else if(data.status == 'password_matches'){
console.log("password matches");
}
},
error: function(jqXHR, textStatus, errorThrown, data){
console.log(jqXHR, textStatus, errorThrown, data);
}
});
$('#btnUpdateInformation').unbind('click');
e.preventDefault();
});
PHP file
if(isset($_POST['userProfileAgeFrom']) && isset($_POST['userProfileAgeTo'])){
$data = array(
$userProfileAgeFrom => $_POST['userProfileAgeFrom'],
$userProfileAgeTo => $_POST['userProfileAgeTo']
);
$user_profile_age_range = $user_home->runQuery("UPDATE nanny_age_range SET age_minimum=:userProfileAgeFrom, age_maximum=:userProfileAgeTo WHERE user_id=:user_id ");
$user_profile_age_range->bindparam(':userProfileAgeFrom', $userProfileAgeFrom, PDO::PARAM_STR);
$user_profile_age_range->bindparam(':userProfileAgeTo', $userProfileAgeTo, PDO::PARAM_STR);
$user_profile_age_range->bindparam(':user_id', $user_id, PDO::PARAM_STR);
$user_profile_age_range->execute();
$response_array['status'] = 'success';
}
And database Table:
EDIT Console.log(formData) shows
UserZipCode
:
"11111"
userBirthAddress
:
"address"
userBirthCity
:
"c"
userBirthCountry
:
"c"
userBirthDay
:
"31"
userBirthMonth
:
"08"
userBirthYear
:
"1992"
userDescription
:
"description"
:
"x"
userLastName
:
"x"
userPassword
:
""
userPasswordConfirm
:
"xxxx"
userPhoneNr
:
"555555555555"
userProfileAgeFrom
:
"["1"]"
userProfileAgeTo
:
"["3"]"
userProfileWage
:
"9.60"
userQuote
:
"c"
userRetypePassword
:
""
userSex
:
"m"
user_id
:
"6"

var_dump($_POST)return ?$userProfileAgeFrom,$userProfileAgeToand$user_iddefined? Your query also written asUPDATErather thanINSERT INTO. I clearly don't understand the question.console.log(formData);