I'm trying to make a loop to add all POST keys to PDO bind Parameter. when i make the POST Request with JS it works however if i made it all in php it don't which i found kind of a strange ,here's an example .
This the JS
var dataString="Name="+encodeURIComponent($("#name ."+Name[i]+"").val())+"&Email="+encodeURIComponent($("#email ."+Name[i]+"").val());
$.ajax({
type: "POST",
url: "sendback.php",
data: dataString,
cache: false,
success: function(html)
{
}
});
PHP Backend.
foreach($_POST as $key => $value) {
$Value=trim(urldecode($value));
$stmt -> bindParam(':'.$key.'',$Value);
}
Result
---------------------------
Name | Email
AAA |[email protected]
----------------------------
This Works and stores data correctly in the database
However
This Don't
$_POST['Name']="AAA";
$_POST['Email']="[email protected]";
foreach($_POST as $key => $value) {
$Value=trim(urldecode($value));
$stmt -> bindParam(':'.$key.'',$Value);
}
$stmt -> execute();
Result
---------------------------
Name | Email
[email protected] |[email protected]
----------------------------
It stores the values of the last $_post key in all the database columns . So all the inputs to the database in this example would be "[email protected]" after searching i found that to make the previous example works i need to add an "&" before the "$value" in the loop,makes me wonder why it's different than the Ajax request,i mean they both POST requests,right?
var dataString={name1: val1, name2: val2,........}