javascript - How to cross check if data is present in the database using ajax method? -
this javascript code checks validation of mobile number data (with other data) , forwards validate_user.php stores mobile number. want store data of users mobile number exists in table or else want display error message saying 'user not present in database'.
i need help. 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 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 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 connect server!".mysql_error()); } mysql_select_db($database) or die("unable select database!".mysql_error()); $sql = mysql_query('select mobile mobile_db mobile="'.$mobile.'"'); if(mysql_num_rows($sql) == 1) { $query = 'insert 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; } ?>
$(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 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 whatever want } else{ alert(json.success) // there made success call 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 connect server!".mysql_error(); } if(empty($json)){ mysql_select_db($database) or die("unable select database!".mysql_error()); $sql = mysql_query('select mobile mobile_db mobile="'.$mobile.'"'); if(mysql_num_rows($sql) == 1) { $query = 'insert 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);
Comments
Post a Comment