Cordova Post Data to Mysql Database using AngularJS Doesn't Work

0

I 've tried it on the web , and it runs correctly , but at the time I implementation in android , post data is not running correctly .

this is the code of controllers

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

$scope.signup = function () {

        var request = $http({
            method: "post",
            url: "http://myurl.com/signup.php",
            crossDomain : true,
            data: {
                email: $scope.email,
                password: $scope.password,
                username: $scope.username
            },
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });
        /* Successful HTTP post request or not */
        request.success(function(data) {
            if(data == "1"){
             $scope.responseMessage = "Successfully Created Account";
            }
            if(data == "2"){
             $scope.responseMessage = "Create Account failed";
            }
            else if(data == "0") {
             $scope.responseMessage = "Email Already Exist"
            }  
        });
}
})

this for my html code

  <ion-view title="Signup">

    <ion-nav-buttons side="left">
      <button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
    </ion-nav-buttons>

    <ion-content class="has-header">
      <div class="list list-inset">

       <label class="item item-input">
         <input class="form-control" type="text" ng-model="username" placeholder="Enter Username">
       </label>

       <label class="item item-input">
         <input type="text" ng-model="email" placeholder="Enter Your Email">
       </label>

       <label class="item item-input">
         <input class="form-control" type="password" ng-model="password" placeholder="Enter Your Password">
       </label>

       <button class="button button-block button-positive button-dark" ng-click="signUp()">SignUp</button><br>
       <span>{{responseMessage}}</span>
     </div>
    </ion-content>

  </ion-view>

and this for my php code

<?php  
// check username or password from database
header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;
$password = $request->password;
$username = $request->username;

$con = mysql_connect('localhost', 'username', 'password') or die ("Could not connect: " . mysql_error());;
mysql_select_db('database_name', $con);

$qry_em = 'select count(*) as cnt from users where email ="' . $email . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);

if($res['cnt']==0){
$qry = 'INSERT INTO users (name,pass,email) values ("' . $username . '","' . $password . '","' . $email . '")';
$qry_res = mysql_query($qry);
    if ($qry_res) {
        echo "1";
    } else {
        echo "2";;
    }
}
else
{
    echo "0";
}
?>

thanks before xD

and this my logcat output

11-26 16:52:46.868: I/v8(17709): [17709]    40790 ms:  Mark-sweep 5.3 (16.9) -> 4.8 (16.9) MB, 0 ms (+ 79 ms in 1 steps since start of marking, biggest step 79.320068 ms) [idle notification: finalize incremental] [GC in old space requested].

11-26 16:52:47.802: W/ResourceType(17709): No package identifier when getting name for resource number 0x00000064

11-26 16:52:47.804: I/View(17709): Touch down dispatch to org.apache.cordova.CordovaWebView{41edb478 VFEDH.C. .F...... 0,0-480,762 #64}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=160.66528, y[0]=351.51312, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16710764, downTime=16710764, deviceId=3, source=0x1002 }
11-26 16:52:47.831: D/AwContents(17709): setContentsSize 320, 508

11-26 16:52:47.832: D/AwContents(17709): setPageScaleFactor 1.0

11-26 16:52:47.900: W/ResourceType(17709): No package identifier when getting name for resource number 0x00000064

11-26 16:52:47.900: I/View(17709): Touch up dispatch to org.apache.cordova.CordovaWebView{41edb478 VFEDH.C. .F...... 0,0-480,762 #64}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=160.66528, y[0]=351.51312, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16710863, downTime=16710764, deviceId=3, source=0x1002 }

11-26 16:52:47.912: D/WebViewCallback(17709): shouldInterceptRequest=http://artivist.gallery/cepjuna/login.php

11-26 16:52:47.913: D/WebViewCallback(17709): onLoadResource=http://artivist.gallery/cepjuna/login.php

11-26 16:52:47.927: D/AwContents(17709): setContentsSize 320, 508

11-26 16:52:47.927: D/AwContents(17709): setPageScaleFactor 1.0

11-26 16:52:48.123: D/AwContents(17709): setContentsSize 320, 508

11-26 16:52:48.123: D/AwContents(17709): setPageScaleFactor 1.0

11-26 16:52:55.522: I/v8(17709): [17709]    49445 ms:  Mark-sweep 5.0 (16.9) -> 3.9 (15.9) MB, 1 / 0 ms (+ 95 ms in 1 steps since start of marking, biggest step 95.473877 ms) [idle notification: finalize incremental] [GC in old space requested].

11-26 16:52:58.865: I/v8(17709): [17709]    52788 ms:  Mark-sweep 4.3 (15.9) -> 3.2 (14.9) MB, 0 ms (+ 49 ms in 1 steps since start of marking, biggest step 49.250977 ms) [idle notification: finalize incremental] [GC in old space requested].

11-26 16:53:06.004: I/v8(17709): [17709]    59927 ms:  Mark-sweep 3.2 (14.9) -> 3.2 (14.9) MB, 0 ms (+ 47 ms in 1 steps since start of marking, biggest step 47.226807 ms) [idle notification: finalize incremental] [GC in old space requested].

11-26 16:53:09.873: I/v8(17709): [17709]    63796 ms:  Mark-sweep 3.2 (14.9) -> 2.9 (14.9) MB, 0 ms [idle notification: finalize idle round] [GC in old space requested].

11-26 16:53:13.910: I/v8(17709): [17709]    67833 ms:  Mark-sweep 2.9 (14.9) -> 2.9 (14.9) MB, 0 ms [idle notification: finalize idle round] [GC in old space requested].

11-26 16:53:24.033: D/dalvikvm(17709): threadid=22: exiting

11-26 16:53:24.033: D/dalvikvm(17709): threadid=22: detach (group=0x41917ce0)

11-26 16:53:24.034: D/dalvikvm(17709): threadid=22: removing from list

11-26 16:53:24.035: D/dalvikvm(17709): threadid=22: bye!

11-26 16:53:35.042: D/dalvikvm(17709): threadid=14: exiting

11-26 16:53:35.042: D/dalvikvm(17709): threadid=14: detach (group=0x41917ce0)

11-26 16:53:35.043: D/dalvikvm(17709): threadid=14: removing from list

11-26 16:53:35.043: D/dalvikvm(17709): threadid=14: bye!
php
android
mysql
angularjs
cordova
asked on Stack Overflow Nov 26, 2014 by Sidiq Fahmi • edited Nov 27, 2014 by Sidiq Fahmi

2 Answers

1

Try this

<ion-nav-buttons side="left">
      <button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
    </ion-nav-buttons>

    <ion-content class="has-header">
      <div class="list list-inset">

       <label class="item item-input">
         <input class="form-control" type="text" ng-model="userdata.username" placeholder="Enter Username">
       </label>

       <label class="item item-input">
         <input type="text" ng-model="userdata.email" placeholder="Enter Your Email">
       </label>

       <label class="item item-input">
         <input class="form-control" type="password" ng-model="userdata.password" placeholder="Enter Your Password">
       </label>

       <button class="button button-block button-positive button-dark" ng-click="signUp(userdata)">SignUp</button><br>
       <span>{{responseMessage}}</span>
     </div>
    </ion-content>

  </ion-view>

In your controller

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

$scope.login = function (userdata) {
    $http.post('http://myurl.com/signup.php',userdata).success(function(data){
           alert(data);
     });
   }
})

php

$postdata = file_get_contents("php://input");
$email = $postdata->email;
$password = $postdata->password;
$username = $postdata->username;
//run your query
answered on Stack Overflow Nov 26, 2014 by iam batman
1

now it's works, i change

  data: {
            email: $scope.email,
            password: $scope.password,
            username: $scope.username
        }

to

 data: {
            'email': $scope.email,
            'password': $scope.password,
            'username': $scope.username
        }

if i don't use '' on data, post data doesn't works.

thanks for your advice @shammon :)

sorry for my bad english. :)

answered on Stack Overflow Nov 27, 2014 by Sidiq Fahmi

User contributions licensed under CC BY-SA 3.0