在错误消息中编码问题

时间:2017-12-10 21:34:32

标签: javascript php jquery ajax validation

当我开始在表单中输入内容时,出现了错误,错误消息格式不正确,请参阅附件: -

我的表单都通过JS / PHP验证。在他们两个中我的错误都是希伯来语。请告知我该如何解决这个问题?

我的js:

$(document).ready(function(){

function jsShow(id) {
    $('#'+id).show();
}

function jsHide(id) {
    $('#'+id).hide();
}

function producePrompt(message, promptLocation, color) {

    $('#'+promptLocation).text(message).css('color', color).show();

}

jQuery.validator.addMethod("lettersonly", function(value, element) {
    return this.optional(element) || /^[a-zא-ת]+(\s[a-zא-ת]+)*$/i.test(value);
}, "Letters only please");
jQuery.validator.addMethod("digitsonly", function(value, element) {
    return this.optional(element) || /([0-9\s\-]{7,})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/.test(value);
}, "Include only digits| min :8 ");

$('.success').hide();

$("#mc-form").validate({
    error: function(label) {
        $(this).addClass( "error" );
    },
    rules: {
        name: {
            required: true,
            lettersonly: true
        },
        phone: {
            required: true,
            digitsonly: true
        },
        email: {
            required: true,
            email: true
        },
        subject: {
            required: true,
            minlength: 2
        },
        message: {
            required: true,
            minlength: 2
        }
    },
    messages: {
        name: {
            required: "שדה זה הינו חובה",
            lettersonly: "אותיות ורווחים בלבד"
        },
        phone: {
            required: "שדה זה הינו חובה",
            digitsonly: "טלפון לא תקין "
        },
        email: {
             required: "שדה זה הינו חובה",
            email: "דואר אלקטרוני לא תקין"
        },
        subject: {
            required: "שדה זה הינו חובה"
        },
        message: {
            required: "שדה זה הינו חובה"
        }
    },
    submitHandler: function(form) {
        sendForm();
    }
});

function sendForm() {

    $('[id*="-error"]').text(''); // default hide all error messages

    event.preventDefault(); // prevent form submission and therefore page reload

    $.ajax({
        type: 'POST',
        url: './send.php',
        dataType: 'json',
        data: $("#mc-form").serialize(),
        success: function(data) {


            if(data.hasOwnProperty('error')) {

                Object.keys(data['error']).forEach(function(key) {
                    producePrompt(data['error'][key], key+'-error', 'red');
                });

            }
            if(data.hasOwnProperty('mail_error')) {
                alert('Could not send mail');
            }
            if(data.hasOwnProperty('success')) {

                 $('.success').show();

                 $("#name").val('');
                 $("#phone").val('');
                 $("#email").val('');
                 $("#subject").val('');
                 $("#message").val('');


            }

        }
    });

}


});

我的php:

 <?php


    $error_msg = array();
    $success_msg = array();
    $data = '';

    // prevent warnings or errors from displaying, else you won't get proper json result
    ini_set('display_errors',0);

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        if (empty($_POST["name"])) {
            $error_msg['name'] = "שדה זה הינו חובה";
        } else {
            $name = test_input($_POST["name"]);
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Zא-ת ]*$/",$name)) {
                $error_msg['name'] = "אותיות ורווחים בלבד";
            }
        }


        if (empty($_POST["email"])) {
            $error_msg['email'] = "שדה זה הינו חובה";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $error_msg['email'] = "דואר אלקטרוני לא תקין";
            }
        }

        if (empty($_POST["phone"])) {
            $error_msg['phone'] = "שדה זה הינו חובה";
        } else {
            $phone = test_input($_POST["phone"]);
            // check if e-mail address is well-formed
            if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
                $error_msg['phone'] = "טלפון לא תקין";
            }
        }
        if (empty($_POST["subject"])) {
            $error_msg['subject'] = "שדה זה הינו חובה";
        }
        if (empty($_POST["message"])) {
            $error_msg['message'] = "שדה זה בינו חובה";
        }

        if (empty($_POST["subject"])) {
            $subject = "";
        } else {
            $subject = test_input($_POST["subject"]);
        }

        if (empty($_POST["message"])) {
            $message = "";
        } else {
            $message = test_input($_POST["message"]);
        }

        if (empty($error_msg)){ // note that $lastname_error does not exists
            $message_body = '';
            unset($_POST['submit']);
            foreach ($_POST as $key => $value){
                $message_body .=  "$key: $value\n";
            }

            $to = 'xxxxx@gmail.com';
            $subjectm = 'Contact Form Submit';
            $feedback = "שם: $name. טלפון: $phone. דואר אלקטרוני :$email. הודעה : $message";
            if (mail($to, $subjectm, $feedback)){
                $success_msg = "הודעתך נשלחה בהצלחה,תודה !";
                $name = $email = $phone = $message = $subject = '';
            } else {
                $mail_error_msg = 'לא ניתן לשלוח אימייל';
            }


        }

        // set output data accordingly
        if(!empty($success_msg)) {
            $data = array('success'=>$success_msg);
        } else if(!empty($error_msg)) {
            $data = array('error'=>$error_msg);
        } else if(!empty($mail_error_msg)) {
            $data = array('mail_error'=>$mail_error_msg);
        }

        // output json that you can parse with jquery
        header('Content-type: application/json; charset=utf-8');
        echo json_encode($data);
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

0 个答案:

没有答案