I am working on an ionic app where the user needs to post data to a MySQL database. Due to some very helpful answers on SO I have gotten around the CORS issues however I have now run into another problem. The submit.php file looks like this:
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$data = json_decode(file_get_contents("php://input"));
$celeb = $data->celeb;
$camp = $data->camp;
$spirit = $data->spirit;
$sport = $data->sport;
$bizs = $data->bizs;
$entrep = $data->entrep;
$young = $data->young;
$conser = $data->conser;
$saty = $data->saty;
$name = $data->name;
$surname = $data->surname;
$email = $data->email;
$contacts = $data->contacts;
$con = mysql_connect('localhost', 'root', 'thePssWrd');
mysql_select_db('theDB', $con);
$qry = 'INSERT INTO test
(celeb,camp,spirit,sport,bizs,
entrep,young,conser,saty,name,
surname,email,contacts)
values ("' . $celeb . '","' . $camp . '",' .$spirit .
','.$sport. ','.$bizs. ','.$entrep. ','.$young.
','.$conser. ','.$saty. ','.$name. ','.$surname.
','.$email. ','.$contacts. ')';
$qry_res = mysql_query($qry);
if ($qry_res) {
if ($qry_res) {
$arr = array('msg' => "Submitted Successfully!!!", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
} else {
$arr = array('msg' => "", 'error' => 'Error In Submit');
$jsn = json_encode($arr);
print_r($jsn);
}
} else {
$arr = array('msg' => "", 'error' => 'This is the big error thing...');
$jsn = json_encode($arr);
print_r($jsn);
}
?>
and my controller:
.controller('FrmController', function ($scope , $http) {
$scope.errors = [];
$scope.msgs = [];
$scope.vote = function() {
$scope.errors.splice(0, $scope.errors.length);
$scope.msgs.splice(0, $scope.msgs.length);
$http.post('http://www.ann7.com/saty/submit.php', {
'celeb' : $scope.celeb,
'camp' : $scope.camp,
'spirit' : $scope.spirit,
'sport' : $scope.sport,
'bizs' : $scope.bizs,
'entrep' : $scope.entrep,
'young' : $scope.young,
'conser' : $scope.conser,
'saty' : $scope.saty,
'name' : $scope.name,
'surname' : $scope.surname,
'email' : $scope.email,
'contacts' : $scope.contacts
}
).success(function(data, status, headers, config) {
if (data.msg != '')
{
console.log(data.msg);
$scope.msgs.push(data.msg);
}
else
{
console.log(data.error);
$scope.errors.push(data.error);
}
}).error(function(data, status) {
$scope.errors.push(status);
});
}
});
If you look at the PHP it is outputting error messages and I am receiving the "This is the big error thing..." error message in my console which I am taking as a successful connection to the DB but an error in inserting the values... my html file looks like as an example:
<form name="nominationForm" ng-controller="FrmController" class="falecomigo novalidate form-manager" >
<div ng-controller="VoteCtrl" class="list list-inset" >
<div class="styled-select">
<select ng-model="bizs">
<option value="e">option1</option>
<option value="1">option1</option>
<option value="2">option1</option>
<option value="3">option1</option>
<option value="4">option1</option>
<option value="5">option1</option>
</select>
</div>
<label class="item item-input">
<input ng-model="name" name="fieldName" type="text" placeholder="Name" required ng-minlength="2" ng-maxlength="70">
</label>
<label class="item item-input">
<input ng-model="surname" name="fieldSurname" type="text" placeholder="Surname" required ng-minlength="2" ng-maxlength="70">
</label>
<label class="item item-input">
<input ng-model="email" name="fieldEmail" type="email" placeholder="E-mail" required ng-maxlength="50">
</label>
<label class="item item-input">
<input ng-model="contacts" name="fieldNumber" type="text" placeholder="Contact No." required ng-minlength="2" ng-maxlength="70">
</label>
</div>
<div class="padding container">
<div class="row">
<button ng-click="vote()" class="button button-balanced button-small col col-100"> Vote </button>
</div>
</div>
</form>
I am not sure where I am going wrong...