Angular js将值从表单传递给php

时间:2015-07-16 06:28:24

标签: javascript php angularjs

我有一个角度形式,它发送值,但phpfile无法在数据库中打印这些值。如果我将静态值放在php文件中,则值将插入到数据库中。我的php文件完全正确。将值从角形式传递到.php文件时出错了。请帮助。

表格:

<form id="form1" post="">

    <div class="form-group">
        <label for="examplename">Name</label>
        <input type="text" class="form-control" id="examplename" placeholder="Enter your name" ng-model="name">
    </div>

    <div class="form-group">
        <label for="exampleemail">Email</label>
        <input type="email" class="form-control" id="exampleemail" placeholder="Enter your email" ng-model="email">
    </div>

    <div class="form-group">
        <label for="examplecontact">Contact No.</label>
        <input type="number" class="form-control" id="examplecontact" placeholder="Enter your contact no." ng-model="contact">
    </div>

    <button type="submit" class="btn btn-default" ng-click="submitting()" >Submit</button>          

</form>

CONTROLLER:

<script>
var app = angular.module('myApp',[]);
app.controller ('myCtrl',function($scope, $http){

    $scope.submitting = function(){
        var request = $http({ method: "post", url: "content/store.php",
        data: {name: $scope.name, email: $scope.email, contact: $scope.contact}
   });
        request.success(function (data) { alert("Successfully data entered! "); });
    }

}); 
</script>

PHP:

$name = $_POST['name'];
$email = $_POST['email'];
$contact  = $_POST['contact'];
echo ($name); 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

$sql = "INSERT INTO table1 (name, email, contact) VALUES ($name , $email, $contact)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

enter image description here

0 个答案:

没有答案