Phantom
Phantom

Reputation: 336

How to cross check if data is present in the database using ajax method?

This is the JavaScript code which checks for the validation of the mobile number data (with other data) and forwards it to validate_user.php which stores the mobile number. But I want to store the data of only those users whose mobile number exists in another table or else I want to display an error message saying 'User not present in the database'.

I need help. What do I do?

Thanks in advance.

JavaScript

$(document).ready(function() {
    $("#user_submit_form").submit(function() {
        var user_data = $("#user_submit_form").serialize();
        var mobile = new Array();
        mobile = $('#mobile').val().split("");

        if (mobile.length != 10 || !(mobile[0] == 7 || mobile[0] == 8 || mobile[0] == 9)) {
            alert('Please enter a valid mobile number');
        } else {
            $.ajax({
                type: "post",
                url: "validate_user.php",
                data: user_data,
                dataType: "json",
            }); // End ajax method
            alert('Thank You');
            window.location.reload();
        }
    });
});

This is the server side PHP code:

<?php 
session_start();

require("config/config.php"); 

    if(isset($_POST['user_submit']))

             $mobile =mysql_real_escape_string ($_POST['mobile']); 
             $dob = mysql_real_escape_string($_POST['dob']);



$hostname = '';
$database = '';
$username = '';
$password = '';


$conn = mysql_connect($hostname,$username,$password);
if(!$conn){
die("Unable to Connect server!".mysql_error());
}
mysql_select_db($database) or die("Unable to select database!".mysql_error());
 $sql = mysql_query('SELECT mobile FROM mobile_db WHERE mobile="'.$mobile.'"');
                if(mysql_num_rows($sql) == 1)
                {
    $query = 'INSERT INTO taj_contact_info (chassis,pin,title,fname,lname,email,mobile,dob,anniversary,company,designation,home_business,add1,add2,city,state,pincode,timestamp) VALUES("'.$mobile.'","'.$dob.'",'.strval(time()).')';

            $sql1= mysql_query($query); 
                }
                    else
                        {
                            return true;
                        }



?>

Upvotes: 0

Views: 2046

Answers (2)

Fisherman
Fisherman

Reputation: 6121

    $(document).ready(function() {
    $("#user_submit_form").submit(function() {
        var user_data = $("#user_submit_form").serialize();
        var mobile = new Array();
        mobile = $('#mobile').val().split("");

        if (mobile.length != 10 || !(mobile[0] == 7 || mobile[0] == 8 || mobile[0] == 9)) {
            alert('Please enter a valid mobile number');
        } else {
            $.ajax({
                type: "post",
                url: "validate_user.php",
                data: user_data,
                dataType: "json",
                success: function(json){
                if(json.error){
                   alert(json.error)// or do whatever you want
                    }
                else{
                   alert(json.success) // there your made a success call the do your staff
                    }
                  }
            }); // End ajax method
            alert('Thank You');
            window.location.reload();
        }
    });
});


**The Server Side php**

    <?php 
session_start();

require("config/config.php"); 

    if(isset($_POST['user_submit']))

             $mobile =mysql_real_escape_string ($_POST['mobile']); 
             $dob = mysql_real_escape_string($_POST['dob']);



$hostname = '';
$database = '';
$username = '';
$password = '';
$json = array();

$conn = mysql_connect($hostname,$username,$password);
if(!$conn){
$json['error'] = "Unable to Connect server!".mysql_error();
}
if(empty($json)){
mysql_select_db($database) or die("Unable to select database!".mysql_error());
 $sql = mysql_query('SELECT mobile FROM mobile_db WHERE mobile="'.$mobile.'"');
                if(mysql_num_rows($sql) == 1)
                {
    $query = 'INSERT INTO taj_contact_info (chassis,pin,title,fname,lname,email,mobile,dob,anniversary,company,designation,home_business,add1,add2,city,state,pincode,timestamp) VALUES("'.$mobile.'","'.$dob.'",'.strval(time()).')';

            $sql1= mysql_query($query);
            $json['success'] = "Successfully inserted"; 
                }
                    else
                        {
                            $json['error'] = 'A Fake Number';
                        }
}

echo json_encode($json);

Upvotes: 4

SagarPPanchal
SagarPPanchal

Reputation: 10111

You can verify this using success response

$.ajax({
                type: "post",
                url: "validate_user.php",
                data: user_data,
                dataType: "json",
                success:(function(result){
                       if(empty(result)){
                          return false;
                       }else{
                         return true;
                       }
                 }));

            }); 

Upvotes: 0

Related Questions