2

I have written a code to send the form data of an ionic application with angularjs to mysql. My code works fine, but only empty records are inserted to mysql These are the codes:

form.html

<div class="list"> 
<form>
<div><input type="text" ng-model="h" ></div>
<div><input type="text" ng-model="s"></div>

<button ng-click='SignUp();'>SignUp</button>
</form>
</div>

App.js

.controller('SearchCntrl', function($scope, $http) {

 $scope.SignUp = function() {

 $http.post('http://www.qatarperfectmedia.com/channel/postdata.php',
 {'h':$scope.h, 's':$scope.s}
 ).success(function(data, status, headers, config) {

    if (data.msg != '')
       {
         $scope.msgs.push(data.msg);
       }
    else
       {
         $scope.errors.push(data.error);
       }
      }).error(function(data, status) {
         $scope.errors.push(status);
       });
}
})

postdata.php

 $data = json_decode(file_get_contents("php://input"));
 $hospital = mysql_real_escape_string($data->h);
 $specialty = mysql_real_escape_string($data->h);
 $qry = 'INSERT INTO doctors (hospital,specialty) 
 values ("'.$hospital.'","'.$specialty.'")';

 $qry_res = mysql_query($qry);
 if ($qry_res) {
    $arr = array('msg' => "User Created Successfully!!!", 'error' => '');
    $jsn = json_encode($arr);
    print_r($jsn);
} else {
    $arr = array('msg' => "", 'error' => 'Error In inserting');
    $jsn = json_encode($arr);
    print_r($jsn);
}

The problem i face is when form submitted, only empty records are being inserted to database. Thnk you

16
  • On your server side do a var_dump($_POST) and check if you are really passing some data Commented Mar 18, 2015 at 13:49
  • 1
    That's beacause you are doing something wrong on your php file, just put this on the top of it var_dump($_POST); Commented Mar 18, 2015 at 14:14
  • 1
    Also mysql functions are deprecated use mysqli instead, check more info here -> php.net/manual/en/function.mysql-real-escape-string.php Commented Mar 18, 2015 at 14:25
  • 1
    At least could you put the var_dump($_POST) so we can see which data your server is receiving? Commented Mar 18, 2015 at 14:31
  • 2
    Here follow this link so you can debug your app -> phonegap-tips.com/articles/… Commented Mar 18, 2015 at 14:44

1 Answer 1

1

I figured it out passing the values via url, and this works perfect

html page

<div class="list"> 
  <form>
    <div><input type="text" ng-model="input.h" ></div>
    <div><input type="text" ng-model="input.s"></div>

    <button ng-click="SignUp(input);">SignUp</button>
  </form>
</div>

Controller

.controller('SearchCntrl', function($scope, $http) {
  $scope.SignUp= function (input){  
    // enter code here
    $http.post("http://www.casda.com/postdata.php?first="+input.h+"&second="+input.s).success(function(data) {
      $scope.tasks = data;
    });
  }
});

postdata.php

// get data from url
if(isset($_GET['first'])){
  $foo= $_GET['first'];
  $foo1= $_GET['second'];
}
// other query code
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.