I have a form, where user can insert up to 4 age ranges. User can add or remove input fields dynamically.
我有一个表单,用户可以插入最多4个年龄范围。用户可以动态添加或删除输入字段。
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
.
我试图将数组发送到我的PHP文件,然后将其插入数据库(为每个范围对插入新行,假设1-3和4-6将在具有正确user_id的单独行上)与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.
回调在PHP文件中是成功的,但没有插入,这意味着它无法从JS获取值,我很遗憾如何使用其余的表单数据发送数组。
In html
I have a button where, user can add fields dynamically and the ID will increment by 1 each time.
在html中我有一个按钮,用户可以动态添加字段,每次ID增加1。
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
EDIT Console.log(formData)显示
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"
2 个解决方案
#1
1
Based on OP comment, console.log(formData)
shows this:
基于OP注释,console.log(formData)显示:
userProfileAgeFrom: "["1"]"
userProfileAgeTo: "["3"]"
It means, now you have a JSON array in your values. So you need to decode it, and get the first value of it:
这意味着,现在您的值中有一个JSON数组。所以你需要解码它,并获得它的第一个值:
$userProfileAgeFrom = json_decode($_POST['userProfileAgeFrom'],true)[0],
$userProfileAgeTo = json_decode($_POST['userProfileAgeTo'],true)[0]
EDIT
编辑
Based on OP comment.
基于OP评论。
So if you have more value in your userProfileAgeFrom
and others, then iterate throught them:
因此,如果您在userProfileAgeFrom和其他人中拥有更多价值,那么请迭代它们:
for ($i = 0; $i < count($_POST['userProfileAgeFrom']); $i++) {
$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', $_POST['userProfileAgeFrom'][$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':userProfileAgeTo', $_POST['userProfileAgeTo'][$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':user_id', $user_id, PDO::PARAM_STR);
$user_profile_age_range->execute();
}
WARNING
警告
But this will bad, because all your rows will be the same because of WHERE user_id=:user_id
但这会很糟糕,因为所有行都是相同的,因为WHERE user_id =:user_id
I think you should redesign your code.
我认为你应该重新设计你的代码。
#2
0
The result of both $userProfileAgeFrom and $userProfileAgeTo are seems to array so the insertion will be work in a for loop. And hope the count of both this array are same.
$ userProfileAgeFrom和$ userProfileAgeTo的结果似乎是数组,因此插入将在for循环中起作用。并希望这个阵列的数量都相同。
$userProfileAgeFrom = $_POST['userProfileAgeFrom'];
$userProfileAgeTo = $_POST['userProfileAgeTo'];
for ($i = 0; $i < count($userProfileAgeFrom); $i++) {
$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[$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':userProfileAgeTo', $userProfileAgeTo[$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':user_id', $user_id, PDO::PARAM_STR);
$user_profile_age_range->execute();
}
#1
1
Based on OP comment, console.log(formData)
shows this:
基于OP注释,console.log(formData)显示:
userProfileAgeFrom: "["1"]"
userProfileAgeTo: "["3"]"
It means, now you have a JSON array in your values. So you need to decode it, and get the first value of it:
这意味着,现在您的值中有一个JSON数组。所以你需要解码它,并获得它的第一个值:
$userProfileAgeFrom = json_decode($_POST['userProfileAgeFrom'],true)[0],
$userProfileAgeTo = json_decode($_POST['userProfileAgeTo'],true)[0]
EDIT
编辑
Based on OP comment.
基于OP评论。
So if you have more value in your userProfileAgeFrom
and others, then iterate throught them:
因此,如果您在userProfileAgeFrom和其他人中拥有更多价值,那么请迭代它们:
for ($i = 0; $i < count($_POST['userProfileAgeFrom']); $i++) {
$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', $_POST['userProfileAgeFrom'][$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':userProfileAgeTo', $_POST['userProfileAgeTo'][$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':user_id', $user_id, PDO::PARAM_STR);
$user_profile_age_range->execute();
}
WARNING
警告
But this will bad, because all your rows will be the same because of WHERE user_id=:user_id
但这会很糟糕,因为所有行都是相同的,因为WHERE user_id =:user_id
I think you should redesign your code.
我认为你应该重新设计你的代码。
#2
0
The result of both $userProfileAgeFrom and $userProfileAgeTo are seems to array so the insertion will be work in a for loop. And hope the count of both this array are same.
$ userProfileAgeFrom和$ userProfileAgeTo的结果似乎是数组,因此插入将在for循环中起作用。并希望这个阵列的数量都相同。
$userProfileAgeFrom = $_POST['userProfileAgeFrom'];
$userProfileAgeTo = $_POST['userProfileAgeTo'];
for ($i = 0; $i < count($userProfileAgeFrom); $i++) {
$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[$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':userProfileAgeTo', $userProfileAgeTo[$i], PDO::PARAM_STR);
$user_profile_age_range->bindparam(':user_id', $user_id, PDO::PARAM_STR);
$user_profile_age_range->execute();
}