• Posting data from Ionic app to PHP server

    1. Create a new blank Ionic project with:
    2. Copy the following code (you’ll already have the .run() part, the .controller() part is novelty here) in www/js/app.js file:
    3. On your server, create a api.php file with the following content:
      As you can see from the code, the first part is explained in detail in this StackOverflow question, and it basically solves the CORS issue that you would otherwise run into.
      The second part, explained in detail in this StackOverflow question,  deals with the way you POST data from Ionic (basically an AngularJS app) to your PHP server. The gist is that AngularJS POSTs by default in a JSON format, and thus you have tojson_decode  the data that comes to your PHP server.
    4. In www/js/app.js file adjust the link variable to point to the file on your server
    5. In www/index.html file copy the following content:
      Here you basically created a form with an username input field and with a submit button. Using AngularJS ng-submit you’re saying that once the submit button is clicked AngularJS should handle it within the submit() function which you defined in app.js file before. Input username uses  the ng-model to bind to the variable on the $scope so that you can then use it in your submit() function.
    6. Run ionic serve  from the root directory of your Ionic app (I’m sure you know this, but just in case that’s where the folders like wwwpluginsscss reside).
  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari