繁体   English   中英

如何将条件警报框放在ajax中

[英]how to put condition alert box in ajax

谁能帮我...我如何在ajax中放置一个条件警报对话框,如果查询中的数据已成功保存或数据已保存。

我想做的是,如果查询已保存,则弹出一个警告框,如果数据已经保存,也会弹出一个对话框。

脚本代码:

<script type="text/javascript">
$(document).ready(function () {
   $('#updates').click(function (e) {
       e.preventDefault();
        var data = {};
        data.region_text = $('#t_region').val();
        data.town_text = $('#t_town').val();
        data.uniq_id_text = $('#t_uniq_id').val();
        data.position_text = $('#t_position').val();
        data.salary_grade_text = $('#t_salary_grade').val();
        data.salary_text = $('#t_salary').val();

        for(var $x=1;$x<=15;$x++) {
            data['id'+$x+'_text'] = $('#id'+$x).val();
            data['aic'+$x+'_text'] = $('#aic'+$x).val();
            data['name'+$x+'_text'] = $('#name'+$x).val();
            data['optA'+$x+'_text'] = $('#optA'+$x).val();
            data['optB'+$x+'_text'] = $('#optB'+$x).val();
            data['optC'+$x+'_text'] = $('#optC'+$x).val();
            data['optD'+$x+'_text'] = $('#optD'+$x).val();
            data['other_qual'+$x+'_text'] = $('#other_qual'+$x).val();
            data['interview'+$x+'_text'] = $('#interview'+$x).val();
            data['total'+$x+'_text'] = $('#total'+$x).val();
        }

        $.ajax({
            type: "POST",
            url: "insert.php",
            data: data,
            cache: false,
            success: function (response) {
            // We are using response to distinguish our outer data variable here from the response
            }
        });

    });
});
</script>

insert.php代码:

<?php
    include('../connection.php');
    date_default_timezone_set('Asia/Manila');  

    $region        = @$_POST['region_text'];
    $town          = @$_POST['town_text'];
    $uniq_id       = @$_POST['uniq_id_text'];
    $position      = @$_POST['position_text'];
    $salary_grade  = @$_POST['salary_grade_text'];
    $salary        = @$_POST['salary_text'];

$dupesql = "SELECT * FROM afnup_worksheet WHERE funiq_id = '$uniq_id'";
$duperow = mysql_query($dupesql);
if(mysql_num_rows($duperow) > 0){
    exit;
}else{

    for($n=1;$n<=15;$n++)   {


    $id           = @$_POST['id'.$n.'_text'];
    $aic          = @$_POST['aic'.$n.'_text'];
    $name         = @$_POST['name'.$n.'_text'];
    $optA         = @$_POST['optA'.$n.'_text'];
    $optB         = @$_POST['optB'.$n.'_text'];
    $optC         = @$_POST['optC'.$n.'_text'];
    $optD         = @$_POST['optD'.$n.'_text'];
    $other_qual   = @$_POST['other_qual'.$n.'_text'];
    $interview    = @$_POST['interview'.$n.'_text'];
    $total        = @$_POST['total'.$n.'_text'];



if(!empty($name)){
$query = "INSERT INTO afnup_worksheet (faic,fregion,ftown,funiq_id,fposition,fsalary_grade,fsalary,fnl_name,edu_attain,experience,seminars,eligibility,other_qual,interview,ftotal,dateinputed) 
VALUES 
('$aic','$region','$town','$uniq_id','$position','$salary_grade','$salary','$name','$optA','$optB','$optC','$optD','$other_qual','$interview','$total',CURRENT_TIMESTAMP)";
$resource = mysql_query($query) or die(mysql_error());
        }
    }
}
?>

只需从PHP返回该状态:

if(mysql_num_rows($duperow) > 0){
    echo "1"; // Dup status
    exit;
}else{
    // All your else code.. echo must be the last thing inside your else block
    echo "2"; // Saved status
}

然后在您的ajax success回调中进行检查:

$.ajax({
    type: "POST",
    url: "insert.php",
    data: data,
    cache: false,
    success: function (response) {
        if (Number(response) == 1)
        {
            alert("Dup message");
        }
        else
        {
            alert("Saved message");
        }
    }
});

而不是退出; 在欺骗的条件下,您可以回显“重复”。 另外,您应该在$ resource之后删除die(),并添加if($ resource)echo“ ok”; 否则回显“错误”;

然后在javascript中的成功函数(响应)中,您可以执行if(response ==“ ...”)echo重复的操作; 否则...

这只是基本的解释,但足以为您指明正确的方向。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM