【问题标题】:How to put an alert box in ajax if all inputs are empty如果所有输入都为空,如何在ajax中放置一个警报框
【发布时间】:2014-02-25 15:36:20
【问题描述】:

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

插入.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){
    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());
        }
    }
}
?>

【问题讨论】:

  • 您必须验证 html 表单,直到使用 Ajax 处理它们
  • 你不能使用required属性吗?

标签: javascript php jquery ajax json


【解决方案1】:

您可以获取所有输入,过滤为空,然后根据该信息弹出警报:

var emptyFields = $(':input').filter(function () { return this.value === '' });

if (emptyFields.length) {
    alert('Fill out all forms fields');
}
else {
    // other code here.
}

如果你采用这种方法,我会围绕整个点击方法来写。这将导致您在尝试处理数据之前验证表单。在表单验证失败的情况下,它可以节省时间;尤其是在这种情况很常见的情况下。

【讨论】:

    【解决方案2】:

    您可以在循环中插入如下变量:

    var bool = true;
    for(var $x=1;$x<=15;$x++) {
        // ... your code ...
        bool = bool && ($('#id'+$x).val() != "");
        // do like the above for all the fields
    }
    // at this point if at least one of your fields is empty, bool will be false
    
    if (bool) {
        // your ajax call here, if all fields are not empty bool == true
    } else {
        alert("Please fill all the fields first");
    }
    

    我希望我得到了你需要的东西,这个问题有点不清楚

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2013-01-06
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多