dongmo20030416 2014-04-30 04:27
浏览 23

PHP中不会显示$ .ajax错误

I'm new to using jQuery with AJAX. I want to build a simple form that prompts the user when one of the field inputs are incorrect.

My only requirement (for now) is that the name must be "John".

html (ajaxtutorial.html)

<!DOCTYPE html>
<html>
<head>
    <title>AJAX Form</title>
</head>
<body>

    <form action="ajax/contact.php" method="post" class="ajax">
        <div>
            <input type="text" name="name" placeholder="Your name">
        </div>
        <div>
            <input type="text" name="email" placeholder="Your email">
        </div>
        <div>
        <textarea name="message" placeholder="Your message"></textarea>
        </div>
        <input type="submit" value="Send">
        <div>
    </form>

    <script src="js/jquery-1.11.0.js"></script>
    <script src="js/main.js"></script>
</body>
</html>

jQuery (main.js):

$('form.ajax').on('submit', function() {
    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[name]').each(function(index, value) {
        var that = $(this), //references the inputs within the find function
            name = that.attr('name'),
            value = that.val();

            data[name] = value;
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        dataType: 'json',
        cache: false,
        success: function(result) {
            if(result.error == true) {
                console.log('Did not type John');
            } 
            else {
                console.log('Typed John');
            }
        }
    }); 
    return false;
});

php (contact.php):

<?php

    $errors = array();
    $form_data = array();

    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    if ($name != 'John') {
    $errors['name'] = true;
    }

    if (array_key_exists('name',$errors)) {
        $form_data['success'] = true;
        $form_data['error'] = true;
    } elseif (empty($errors)) {
        $form_data['success'] = true;
    }
    echo json_encode($form_data);
?>

I feel it's simple, but can't solve it. I want to identify the error by it's class (i.e. result.['class']) in order to provide unique feedback for each error.

Thanks for the help

  • 写回答

1条回答 默认 最新

  • dongshimao7115 2014-04-30 04:49
    关注

    Try to use serialize() instead of looping like,

    $('form.ajax').on('submit', function() {
        var that = $(this),
            url = that.attr('action'),
            type = that.attr('method');  
    
        $.ajax({
            url: url,
            type: type,
            data: that.serialize(),// use form.serialize() here
            dataType: 'json',
            cache: false,
            success: function(result) {
                if(result.error == true) {
                    console.log('Did not type John');
                } 
                else {
                    console.log('Typed John');
                }
            }
        });        
        return false;    
    });
    

    Also in PHP in case of error, your are assigning success and error both try it like,

    if (array_key_exists('name',$errors)) {
        // remove success key from here
        $form_data['error'] = true;// only error
    } elseif (empty($errors)) {
        $form_data['success'] = true; // only success
    }
    echo json_encode($form_data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line