0

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



?>
4
  • use the success callback of ajax request and set logic regarding response returned by server. You should be able by searching to find hundred of duplicates of similar question... Commented Mar 6, 2014 at 11:20
  • any code that you tried on server side? Commented Mar 6, 2014 at 11:20
  • as you have a ajax method so you have to check it on server side php as you have your environment and there you have to do your logic then return your message to your ajax method and show that in your ajax callback. Commented Mar 6, 2014 at 11:28
  • Does your server-side code work? Do you have a return for the case where you find the mobile number? Commented Mar 6, 2014 at 11:39

2 Answers 2

4
    $(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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

            }); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.