甜蜜警报确认然后将数据保存到数据库

时间:2017-01-11 03:06:58

标签: javascript php sweetalert

我想将数据保存到数据库,但在此之前我必须检查名称条件。如果名称可用,它将显示确认弹出(我使用甜蜜警报)。我已经尝试过这段代码,但它不能执行保存数据的功能。有人可以帮帮我吗?

代码:

 $name = $_POST['name'];
 $cekname = mysql_query("select * from data_alumni where name='$name'");
 $jumlahnama = mysql_num_rows($cekname);

 if ($jumlahname > 0) {
     echo '<script>swal({ title: "Are you sure?", text: "name is available, continue save?", type: "warning", showCancelButton: true, confirmButtonColor:"#DD6B55",  confirmButtonText: "Yes, delete it!",   closeOnConfirm: false }, function(){' . savedata($nama) . '});</script>';
 } else { 
     #other function 
 }

2 个答案:

答案 0 :(得分:0)

  1. 检查您的查询 - 您必须使用点运算符(即.)连接变量:
     $ceknama = mysql_query("select * from data_alumni where nama='".$nama."'");
  2. 在AJAX调用之后使用Javascipt confirm()方法与PHP脚本和数据库进行交互式通信。

答案 1 :(得分:0)

Php(客户端):

 document.querySelector('#form-input-biodata').addEventListener('submit', function (e) {
    var form = this;
    e.preventDefault();
    var nama = $("#nama-alumni").val();
    var ceknama = 'ceknama=' + nama;
    $.ajax({
        type: "POST",
        url: "url file php",
        data: ceknama,
        cache: false,
        success: function (jumlahnama) {
            if (jumlahnama == "0") {
                form.submit();

            } else {
                swal({
                    title: "Konfirmasi?",
                    text: "Nama telah terdaftar, Ingin Melanjutkan?!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Yes",
                    closeOnConfirm: false
                },
                        function () {
                            form.submit();
                        });
            }
        }
    });
});

服务器端:

if (isset($_POST['ceknama'])) {
include 'connection.php';
$nama = isset($_POST['ceknama']) ? addslashes($_POST['ceknama']) : "";
$hubung = mysql_query("its my query");
$total = mysql_num_rows($hubung);
echo $total;}