我结合了html、php和ajax在mysql数据库中插入数据。我想使用sweetealert在ajax成功函数中显示成功或错误消息。数据正在插入数据库,但无法显示消息。
以下是我的代码:
插入.php

<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("hotelmanagement", $connection);

$fName = $_POST['fName'];
$lName = $_POST['lName'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$phone =  $_POST['phone'];
$email =  $_POST['email'];
$checkInDate =  $_POST['checkInDate'];
$checkOutDate =  $_POST['checkOutDate'];
$adults =  $_POST['adults'];
$children =  $_POST['children'];
$specialInstructions =  $_POST['specialInstructions'];
$query = mysql_query("INSERT INTO reservation(FirstName,LastName,Address1,Address2,Phone,Email,Adults,Children,CheckInDate,CheckOutDate,SpecialInstructions) VALUES('$fName','$lName','$address1','$address2','$phone','$email','$checkInDate','$checkOutDate','$adults','$children','$specialInstructions')");

echo json_encode($query);

mysql_close($connection);

?>
这是我的ajax代码:
$("#submit").click(function(){
    var fName = $("#fName").val();
    var lName = $("#lName").val();
    var address1 = $("#address1").val();
    var address2 = $("#address2").val();
    var phone = $("#phone").val();
    var email = $("#email").val();
    var checkInDate = $("#checkinDate").val();
    var checkOutDate = $("#checkoutDate").val();
    var adults = $("#adults").val();
    var children = $("#children").val();
    var specialInstructions = $("#specialInstructions").val();


    if(fName == '' || lName == '' || phone == ''){
        swal("Oops!!", "Looks like you missed some fields. Please check and try again!", "error");
    }else{
        $.ajax({
            type:'post',
            url:'insert.php',
            data: {fName:fName,lName:lName,address1:address1,address2:address2,phone:phone,email:email,checkInDate:checkInDate,checkOutDate:checkOutDate,adults:adults,children:children,specialInstructions:specialInstructions},
            dataType:'json',
            succcess:function(data){
                swal("Success", "Data Saved Successfully", "success");
            },
            error:function(xhr, thrownError, ajaxOptions){

            },
        });
    }
});

你能告诉我我缺了什么吗。谢谢您。

最佳答案

您的成功回调是三次ccc并尝试删除dataType:'json',
尝试此ajax请求

$.ajax({
            type:'post',
            url:'insert.php',
            data: {fName:fName,lName:lName,address1:address1,address2:address2,phone:phone,email:email,checkInDate:checkInDate,checkOutDate:checkOutDate,adults:adults,children:children,specialInstructions:specialInstructions},

            success:function(data){
                swal("Success", "Data Saved Successfully", "success");
            },
            error:function(xhr, thrownError, ajaxOptions){

            }
       });

10-05 20:37
查看更多