从html到php服务器的离子应用程序数据 [英] Ionic app data from html to php server

查看:159
本文介绍了从html到php服务器的离子应用程序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ionic框架的新手,我很难将数据从HTML发布到PHP。

I'm new to Ionic framework and am struggling to post data from HTML to PHP.

1.index.html:

1.index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->


    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
  <script src="js/ng-cordova.min.js"></script>
    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>


    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">HTML</h1>
      </ion-header-bar>

      <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="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>
    </ion-pane>
  </body>
</html>




  1. app.js:

  1. app.js:

.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"
            }  
        });
}
})


3.PHP文件:

<?php
        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 = $postdata->email;
        $password = $postdata->password;
        $username = $postdata->username;

        $con = mysql_connect("localhost","username",'password') or die ("Could not connect: " . mysql_error());;
        mysql_select_db('dbname', $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";
        }
        ?>


推荐答案

编辑:我写的关于如何将数据从Ionic发布到PHP的详细帖子,您可以查看它这里

edit: I wrote a detailed post on how to post data from Ionic to PHP and you can view it here.

index.html 你有这样的文件:

ng-click="signUp(userdata)

但是在 app.js 文件中你有这个:

but then in the app.js file you have this:

$scope.signup = function () {

而不是像:

$scope.signup = function (userdata) {

如果您选择此路线,那么您的数据应如下所示:

If you would go this route then your data should look like this:

data: {
        email: userdata.email,
        password: userdata.password,
        username: userdata.username
}

然而,你不必这样做!您可以这样做,以便从 ng-click =signUp(userdata)中删除​​ userdata ,然后在您执行此操作的数据部分中的app.js文件:

However, you don't have to do it like this! You can do it like so that you remove the userdata from ng-click="signUp(userdata) and then in the app.js file in the data part you do it like this:

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

这篇关于从html到php服务器的离子应用程序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆