Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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

?>

share|improve this question
    
Add your php code – Rajkumar R Jul 18 at 10:05
    
simple php code – Keshav Desai Jul 18 at 13:12
    
<?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); ?> – Keshav Desai Jul 18 at 13:12

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

         
			
			});

share|improve this answer
    
Not connecting to db. i added dbname and table name correctly . still not connecting. – Keshav Desai Jul 18 at 13:57
    
Can you please share the error output from the server? – JRoller Jul 18 at 14:00
    
where do i find that? is it from network tab in developer tool? – Keshav Desai Jul 18 at 15:06
    
data is posting getting 200 ok . header payload contains data . but not storing in database and not connecting it – Keshav Desai Jul 19 at 4:00
    
now data is inserting. But it displays there was an error in connecting database. Other two conditions are not working. – Keshav Desai Jul 19 at 4:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.