2016-07-18 5 views
0

私はangularjsフォームを作成しました。 PHPを使用してフォームの値をデータベースに保存し、挿入する前に電子メールがすでに存在するかどうかを確認したいと思います。私はPHPが初めてです。どんな助けもありがとう。ありがとう。PHPを使ってangularjsフォームの値をデータベースに挿入

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コード。

<?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); 
 

 
?>

+0

追加あなたのPHPコード –

+0

単純なPHPコード –

+0

username; $ email = $ data-> email; $ password = $ data-> password; $ con = mysql_connect( "localhost"、 "root"、 ""); mysql_select_db( "userregister");$ sql = "ユーザー名(ユーザー名、電子メール、パスワード)($ username、 '$ email'、 '$ password')に値を挿入します。 $ result = mysql_query($ sql); ?> –

答えて

0

また、あなたは、変数$ DBNAMEを作成し、それに右のDBNAMEを割り当てる必要があります注意してください(次のようにmysqliのを使用してみてください:

$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"; 
} 

あなたもする必要があります。可能なイベントに合わせてJavascriptを変更してください:

+0

dbに接続していません。私はdbnameとテーブル名を正しく追加しました。まだ接続していません。 –

+0

サーバーからのエラー出力を共有できますか? – Shizzle

+0

どこですか?開発者ツールのネットワークタブからですか? –

関連する問題