且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的错误:未捕获的类型错误:无法读取未定义的属性“长度”

更新时间:2022-10-29 17:13:27

我想这是因为邮件函数失败,并返回这里:

 如果(电子邮件($到,$主题,$味精,$头)){
   $消息['成功'] ='成功';
}
 

让你的回应既没有成功属性,也不错误属性,这会导致这里的错误:

  $。每个(response.error,功能(键,值){
   如果(值){
      $('#'+键+_error)HTML(值);
   }
});
 

您可以尝试这样的:

 如果(电子邮件($到,$主题,$味精,$头)){
    $消息['成功'] ='成功';
}其他{
    $消息['错误'] ['邮件'] ='邮件的错误!;
}
 

或者设置 $消息['成功'] ='成功'; 后直接将数据保存到表姑邮件成功

I have a form to call ajax for back-end processing, all input will stored in table and will return 'success' to to notify user the submission is success. But I facing an issue during callback, input data can saved into table but the callback is halted with error below, I have no idea what is goes wrong, the same script was applied to another form (with different form field) are working fine.

console log - Chrome:

Uncaught TypeError: Cannot read property 'length' of undefined
m.extend.each
$.ajax.success
j
k.fireWith
x
b

Firefox

TypeError: a is undefined

...rCase()},each:function(a,b,c){var d,e=0,f=a.length,g=r(a);if(c){if(g){for(;f>e;e...

firefox error is pointed to f=a.

Script:

$(document).ready(function() {

        $("#status").hide();

        $('#btn_submit').click(function(){
            var params = $('#project_form').serialize();
            var btn = $(this);
            btn.button('loading')

            $.ajax({
                url: baseurl + '/process_form.php',
                type: 'POST',
                data: params,
                dataType: 'json',
                success: function(response){
                    if(response.success == 'success'){

                        $('#status').html('<b>Thank you</b>').show();

                        $('html,body').animate({
                            scrollTop: $('#top').offset().top
                        },500);

                    }else{
                        $('[id$="_error"]').html('');
                        $.each(response.error, function(key, value){
                            if(value){
                                $('#' + key + '_error').html(value);
                            }
                        });
                    }
                },
                error: function(){
                    console.log(arguments);
                }
            }).always(function(){
                btn.button('reset')
            });

        });

});

process_form.php

header("Content-Type: application/json");

$serialized_data = serialize($_POST);

$fullname = $mysqli->real_escape_string(ucwords(strtolower($_POST['fullname'])));
$age = $mysqli->real_escape_string(ucwords(strtolower($_POST['age'])));
$email = $mysqli->real_escape_string(strtolower(trim($_POST['semail'])));
$phone = $mysqli->real_escape_string(trim($_POST['phone']));
$proj_id = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($_POST['project_id']));
$proj_name = $mysqli->real_escape_string(ucwords(strtolower($_POST['project_name'])));
$agent_id = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($_POST['agent_id']));
$agent_email = $mysqli->real_escape_string(strtolower(trim($_POST['agent_email'])));
$agent_name = $mysqli->real_escape_string(ucwords(strtolower($_POST['agent_name'])));
$captcha = $_POST['captcha_code'];

$func = new Functions();
$message = array();

// validate
if(empty($fullname) || strlen($fullname) < 2){
    $message['error']['fullname'] = 'Your name is required';
}
if(empty($age)){
    $message['error']['age'] = 'Your age is required';
}
if(!$func->check_email_address($email)){
    $message['error']['semail']  = 'Invalid email address';
}
if(empty($phone) || strlen($phone) < 10){
    $message['error']['phone'] = 'Phone num is required';
}

$securimage = new Securimage();
$securecode = $securimage->getCode();
if(strtolower($securecode) != strtolower($captcha)) {
    $message['error']['captcha'] = 'Incorrect security code';
}


if(!isset($message['error'])){

    // insert table
    $create = $mysqli->query("INSERT INTO `xxx` ...") or die($mysqli->error);

    if($create){
        //send email
        $to = xxx;
        $subject = 'yyy';
        $msg = '<html>
                    <body>
                        blah...
                    </body>
                </html>';
        $from = zzz;
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers .= 'From: '.$fullname.' <'.$from.'>' . "\r\n";

        if(mail($to, $subject, $msg, $headers)){
            $message['success'] = 'success';
        }
    }

}

echo json_encode($message);

I think it is because the mail function failed, and returned false here:

if(mail($to, $subject, $msg, $headers)){
   $message['success'] = 'success';
}

so your response neither has success property, nor error property, and this causes an error here:

$.each(response.error, function(key, value){
   if(value){
      $('#' + key + '_error').html(value);
   }
});

You can try something like:

if(mail($to, $subject, $msg, $headers)){
    $message['success'] = 'success';
}else{
    $message['error']['mail'] = 'Mail error!';
}

or set $message['success'] = 'success'; directly after saving data into table regardless mail success.