使用Ajax代码在数据插入数据库时​​显示弹出消息

时间:2014-03-06 05:44:29

标签: javascript php jquery ajax

提交表单时,我需要检查数据库中是否存在机箱和引脚。如果存在,我需要通过Ajax显示弹出消息。这是我的代码。但是如果数据已被插入或者有任何错误,我没有收到任何弹出消息。你们可以帮我弄清楚我哪里出错了吗?在此先感谢您的帮助。

AJAX CODE :
<script type="text/javascript">
$(document).ready(function () {

   $("#user_submit_form").submit(function () {
       var user_data = $("#user_submit_form").serialize();

       if ($('#chassis').val() == '') {
           alert('Please enter chassis');
       } else if ($('#pin').val() == '') {
           alert('Please enter pin');
       } else

       {
           $.ajax({
               type: "post",
               url: "validate_user.php",
               data: user_data,
               dataType: "json",
               success: function (user_data) {
                   if (user_data == "Data inserted") {
                       alert("Data inserted");
                   } else {
                       alert("fail!");
                   }
               }

           }); // End ajax method

       }
   });

});         
</script>

PHP CODE:
<?php 
session_start();


$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 chassis,pin FROM checking_chassis WHERE chassis="'.$chassis.'" && pin="'.$pin.'" ');
                if(mysql_num_rows($sql) == 1)
{
            echo "Data inserted";   
}
else
{       
    echo "Error";

}

?>

2 个答案:

答案 0 :(得分:0)

在提交并检查后首先执行此操作

$("#user_submit_form").submit(function(e){
       e.preventDefault();

});

并提醒return false;  要么 像这样传递数据

$data = array(
   'status'  => 1,
   'message' => 'Data inserted'
);
echo json_encode($data);

并成功使用

var json = $.parseJSON(user_data);

alert(json.message);

答案 1 :(得分:0)

I am trying this now but I don't think that the value is getting returned. It is just printing the content of the else statement    

<script type="text/javascript">
        $(document).ready(function (){

            $("#user_submit_form").submit(function(){
                var user_data = $("#user_submit_form").serialize();

            var mobile = new Array();               
                mobile = $('#mobile').val().split("");

                var pincode = new Array();              
                pincode = $('#pincode').val().split("");

                if($('#chassis').val() =='')
                {           
                        alert('Please enter chassis');
                }   
                else if($('#pin').val() =='')
                {           
                        alert('Please enter pin');
                }   

                else 

                {
                    $.post("validate_user.php",{"chassis":$('#chassis').val(),"pin":$('#pin').val(),"title":$('#title').val(),"fname":$('#fname').val(),"lname":$('#lname').val(),"email":$('#email').val(),"mobile":$('#mobile').val(),"dob":$('#dob').val(),"anniversary":$('#anniversary').val(),"company":$('#company').val(),"designation":$('#designation').val(),"home_business":$('#style').val(),"add1":$('#add1').val(),"add2":$('#add2').val(),"city":$('#city').val(),"state":$('#state').val(),"pincode":$('#pincode').val()},function(data) {

                    if(data == true)
                    {
                            alert("Error");

                    }
                            else 
                            {

                            alert("Success");

                            }


                    });                                     
                }
            });     

        });


    </script>