dongyounai6281 2014-02-25 15:36
浏览 13
已采纳

如果所有输入都为空,如何在ajax中放置一个警告框

i would like to know how to put an alert box if all the variables are empty in ajax? i mean when i click the button and all the fields are empty it will pop-op and say please fill the field first...how can i do that?

current script:

<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) {
            if (Number(response) == 1)
        {
    alert("cant saved again!");
        }
        else
        {
    alert("successfully saved!");
        }
            }
        });
    });
});
</script>

insert.php code:

<?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){
    echo "1";
    exit;
}else{
    echo "2";
    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());
        }
    }
}
?>
  • 写回答

2条回答 默认 最新

  • doucai1901 2014-02-25 16:01
    关注

    You can grab all inputs, filter for empty, and pop the alert based on that:

    var emptyFields = $(':input').filter(function () { return this.value === '' });
    
    if (emptyFields.length) {
        alert('Fill out all forms fields');
    }
    else {
        // other code here.
    }
    

    If you take this approach, I would write it around the entire click method. This would result in you validating the form, before making any attempt to process the data. It may save time, in the instance where the form fails validation; especially if it is a common occurrence.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境