drv54591 2016-07-18 10:03
浏览 57

使用php将angularjs表单值插入数据库

I have created angularjs form. I want to store the form values into data base using PHP and before inserting I want to check weather the email is already exists or not. I am new to PHP. Any help would be appreciated. Thanks.

Register.html:

<div class="container col-lg-10" style="margin-top:2em; margin-left:2em;" >
    <div class="panel panel-default">
     <div class="panel-body" ng-app="TempleWebApp" ng-controller="RegisterCtrl">

       <form name="userForm" ng-submit="submitForm()" novalidate>

                <!-- NAME -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && (userForm.name.$dirty || submitted)}">
                    <label>Name</label>
                    <input type="text" name="name" class="form-control" ng-model="user.name" placeholder="Your Name" ng-required="true">
                    <p ng-show="userForm.name.$error.required && (userForm.name.$dirty || submitted)" class="help-block">You name is required.</p>
                </div>

                  <!-- EMAIL -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && (userForm.email.$dirty || submitted)}">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" ng-model="user.email" placeholder="Your Email Address" ng-required="true">
                    <p ng-show="userForm.email.$error.required && (userForm.email.$dirty || submitted)" class="help-block">Email is required.</p>
                    <p ng-show="userForm.email.$error.email && (userForm.email.$dirty || submitted)" class="help-block">Enter a valid email.</p>
                </div>

                <!-- PASSWORD -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.password.$invalid && (userForm.password.$dirty || submitted)}">
                    <label>Password</label>
                    <input type="Password" name="password" class="form-control" ng-model="user.passwrd" placeholder="Your Password" ng-required="true">
                    <p ng-show="userForm.password.$error.required && (userForm.password.$dirty || submitted)" class="help-block">Your password is required.</p>
                </div>

                <!-- TERMS & CONDITIONS -->
                <div class="form-group" ng-class="{ 'has-error' : userForm.terms.$invalid && (userForm.terms.$dirty || submitted)}">
                    <label>Accept Terms & Conditions</label>
                    <input type="checkbox" value="" name="terms" ng-model="user.terms" ng-required="true" />
                    <p ng-show="userForm.terms.$error.required && (userForm.terms.$dirty || submitted)" class="help-block">Accept terms & conditions.</p>
                </div>

                <!-- ng-disabled FOR ENABLING AND DISABLING SUBMIT BUTTON -->
                <!--<button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid">Register</button>-->
                <button type="submit" class="btn btn-primary col-lg-offset-6">Register</button>

            </form>

            <pre>{{user}}

            </pre>
      </div>
     </div>
  </div>

Main.js:

var app = angular.module('TempleWebApp', [ 'ngRoute']);

app.controller('RegisterCtrl', function ($scope,$location, $http) {

  $scope.user = {};
  $scope.user.name= "" ;
  $scope.user.email ="";
  $scope.user.passwrd="";
  $scope.user.terms="";

    // function to submit the form after all validation has occurred
    $scope.submitForm = function () {

        // Set the 'submitted' flag to true
        $scope.submitted = true;

       $http.post("register.php",{'username':$scope.user.name,'email':$scope.user.email,'password':$scope.user.passwrd})
        .success(function(data,status,headers,config){
            console.log("Inserted Successfully!");
            });

    };
});

PHP code.

<?php
$data = json_decode(file_get_contents("php://input"));
$username = $data->username;
$email = $data->email;
$password = $data->password;
$con = mysql_connect("localhost","root","");
mysql_select_db("userregister");
$sql = "insert into user(username,email,password) values($username,'$email','$password')";
$result = mysql_query($sql);

?>

</div>
  • 写回答

1条回答 默认 最新

  • duanhuang4841 2016-07-18 13:28
    关注

    Try using mysqli in the following manner (Also note you should create the variable $dbname and assign the right dbname to it:

    $data = json_decode(file_get_contents("php://input"));
    $username = @$data->username;
    $email = @$data->email;
    $password = @$data->password;
    $dbname = '';
    $conn = new mysqli("localhost","root","",$dbname);
    
    $check = "SELECT * FROM user WHERE email='$email'";
    
    //The following rows check whether this email already exists in the DB
    
    $results = $conn->query($check);
    if($results && mysqli_num_rows($results)>0)
    {
        echo "email";
        die;
    }
    
    //The following rows will work only if there is no such email in the DB
    
        if($conn->connect_error)
    {
        echo "false";
        die;
    }
    
    $sql = "INSERT INTO user VALUES values($username,'$email','$password')";
    
    if ($conn->query($sql) === true)
    {
        echo "true";
    }
    

    You will also need to change your Javascript to fit the possible events:

           $http.post("register.php",{'username':$scope.user.name,'email':$scope.user.email,'password':$scope.user.passwrd})
            .success(function(data,status,headers,config){
             if(data == 'true'){
               console.log("Inserted Successfully!");
               }
             else if(data == 'email'){
               console.log("The email already exists");
             }
             else{
               console.log("There was an issue connecting to the DB");
             }
    
             
                
                });

    </div>
    
    评论

报告相同问题?