I am trying to send data from my ionic app to mysql. The problem is when I click on the submit button, it actually post empty records in the database.
My HTML code
<form >
<label class="item item-input">
<span class="input-label">Product Name:</span>
<input type="text" ng-model="productname">
</label>
<label class="item item-input">
<span class="input-label">Product Price:</span>
<input type="number" ng-model="productprice" >
</label>
<label class="item item-input">
<span class="input-label" >Description:</span>
<input type="text" ng-model="productdescription">
</label>
<input type="button" value="Submit" class="button button-assertive button-block" ng-click="submit()">
</form>
The controller for the project
App.controller('appCtrl', ['$scope', '$http', '$state, function ($scope,
$http, $state, $ionicPopup, $timeout) {
$scope.submit = function (){
var link = "http://localhost/api.php";
$http.post(link, {
'productname' : $scope.productname,
'productprice' : $scope.productprice,
'productdescription' : $scopeproduct.description,
}).success(function(response){
$scope.pdata= response;
console.log(response);
//console.log(response);
})
}
}]);
This is my PHP code to post the the data to database
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "root", "", "ionic");
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
$postdata = file_get_contents("php://input");
if(isset($postdata) && !empty($postdata))
{
$request = json_decode($postdata);
$pname = $request->productname;
$pprice = $request->productprice;
$pdescription = $request->productdescription;
$sql = ("INSERT INTO `users` ( product_name, product_price, description) VALUES ('$pname', '$pprice', '$pdescription')");
mysqli_query($conn, $sql);
}
?