Reputation: 794
I have two html text input fields and two buttons.
The first button saves the value pairs from the two input fields in an array. The second button sends this array with all the input value pairs to a php file and the php file saves all these values in a database. The user can input values so much as he can, there is no limit.
<input type="text" id="field1" name="field1" />
<input type="text" id="field2" name="field2" />
<button id="notizeValues">Notize</button>
<button id="sendToServer">Save</button>
My question is, how can the server access this data? I am trying this:
var values = [];
$('#notizeValues').click(function (){
var value1 = $('#field1').val();
var value2 = $('#field2').val();
values.push({'value1': value1 , 'value2': value2 });
});
$('#sendToServer').click(function (){
var sendValues = values;
$.post("achieveAdminAktion.php",{values: sendValues }, function(data){
$("#serverResponse").html(data);
});
});
Now comes the code of the php file, but I get the whole time errors.
For this solution I get an Warning: json_decode() expects parameter 1 to be string, array given
and errors in echo function:
isset($_POST['values']){
$values = json_decode($_POST['values'], true);
foreach ($values as $json) {
....save value in database....
}
echo $values; // nothing
}
That is only one example of lot of examples I tried but nothing works. How can I solve this problem? How can I access these data in php file and how can I walk through the json-array and save al these value pairs in database.
Upvotes: 3
Views: 495
Reputation: 38298
$.post()
doesn't JSON-encode variables sent to the server - so the json_decode
call in PHP is unnecessary.
Upvotes: 2