dsmnedc798226 2017-03-31 15:18
浏览 76

如何从angularjs和ionic发送数据到php

I am an angular begginer, and I am currently trying to save some data in a data base with the $http() service, but it does not work. Could you help me, please? This is what I am trying to do:

Form:

<form name="form">
  <input ng-model="model.text" type="text" name="text">
  <button class="button" ng-click="saveData()"> Save </button>
</form>

Controller of that page:

.controller('pagCtrl', function ($scope, $http) {
  $scope.saveData = function () {

    $http ({
        url : 'http://localhost/prueba/www/php/enviar_datos.php',
        method : 'POST',
        data : { text : $scope.form.text.value }
    }).then (function (response) {

    }, function (response) {

    });
  }
})

PHP:

<?php
    $link = mysqli_connect("localhost", "root", "", "testDB");

    if (mysqli_connect_errno()) {
        die("Error de conexión: " . mysqli_connect_error());
    }

    mysqli_set_charset($link, 'utf8');
    mysqli_query($link, "INSERT INTO tabla_prueba (texto) VALUES ('" . $_POST['text'] . "')");
    mysqli_close($link);
?>

I think the problem is that it does not connect correctly to the php url. I am working with Ionic. Thank you for your attention!

  • 写回答

2条回答 默认 最新

  • duan3601 2017-03-31 15:30
    关注

    add $scope.form.text={} and in your js

    data:$.param($scope.form.text);

    and remove ng-click in button put ng-submit in form ng-submit="saveData()"

    So you should rename your model . it hard to read.

    ng-model="formData.text"
    

    and in js

    $scope.formData ={}
    data:$.param($scope.formData)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示