I have two questions:
Q1) How can I pass array values into PHP and echo out same values in PHP? Q2) How to insert into values into database where I array key is the name of MySQL field and array value is the data for that particular field?
My console.log shows following output:
[]
firstname: "apple"
lastname: "banana"
length: 0
But I don't get these values displayed with PHP....
I have an HTML form which displays custom fields:
<form>
<div ng-repeat="field in fields">
<div ng-if="field.required == 'yes'">
<label>
{{ field.fieldlabel }}
</label>
<input type = "{{ field.fieldtype }}" ng-model="values[field.fieldname]">
</div>
</div>
<div id ="entryform-button">
<input type="submit" class="btn-primary btn" id="save" name="submit" value = "Save" ng-click="insertTrans()">
</div>
</form>
And Angular controller which pass data to PHP:
$scope.values = [];
$scope.insertTrans = function () {
console.log($scope.values);
//JSON.stringify($scope.values)
$http.post(
"model/Form.php",
{ 'formvalues' : $scope.values }
).then (function(data) {
alert (data.data);
});
}
But in PHP I have tried json_decode, splash, and try to display with var_dump, echo, print_r but unable to see the data....
<?php
$data = json_decode(file_get_contents("php://input"));
//$data = json_decode($_POST['formvalues'], ture);
if(count($data) > 0) {
var_dump($data);
//print_r($data['formvalues']);
//echo $data['formvalues'];
//echo implode(" ", $data);
//print_r (explode(" ",$data));
// foreach ($data['formvalues'] as $value) {
// echo $value;
// }
// $sql= "INSERT INTO trans ( ) VALUES ( )";
// $conn->exec($sql);
// echo "Data Inserted into Database";
}
PHP returns:
array(1) {
["formvalues"]=>
array(0) {
}
}
$scope.valuesarray at the point of you posting that data$scope.values = [];and see if it works.