如何修复带有多个下拉菜单的联系表单的未定义变量错误?

时间:2019-06-18 17:11:59

标签: javascript php html css arrays

我正在制作包含多个下拉菜单的联系表格。我是php新手。当我准备好一切并放在网站上时,它将不起作用。当我按提交时,我得到

{
"nameMessage":"",
"emailMessage":"",
"phoneMessage":"",
"artistMessage":"",
"tattooMessage":"",
"sizeMessage":"",
"budgetMessage":"",
"placeMessage":"",
"messageMessage":""
} 

,没有消息发送。我该如何解决这个问题,使我不会收到此错误并发送消息?

/*
    Contact form
*/
$('.c-form-box form').submit(function(e) {
    e.preventDefault();
    var this_form_parent = $(this).parents('.c-form-box');
    var postdata = $(this).serialize();
    $.ajax({
        type: 'POST',
        url: './assets1/contact.php',
        data: postdata,
        dataType: 'json',
        success: function(json) {
 
            $('.c-form-box form label[for="c-form-name"] .contact-error').fadeOut('fast', function(){
                if(json.nameMessage != '') {
                    $(this).html('(' + json.nameMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-email"] .contact-error').fadeOut('fast', function(){
                if(json.emailMessage != '') {
                    $(this).html('(' + json.emailMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-phone"] .contact-error').fadeOut('fast', function(){
                if(json.professionMessage != '') {
                    $(this).html('(' + json.professionMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-artist"] .contact-error').fadeOut('fast', function(){
                if(json.subjectMessage != '') {
                    $(this).html('(' + json.subjectMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-get"] .contact-error').fadeOut('fast', function(){
                if(json.subjectMessage != '') {
                    $(this).html('(' + json.subjectMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-size"] .contact-error').fadeOut('fast', function(){
                if(json.subjectMessage != '') {
                    $(this).html('(' + json.subjectMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-budget"] .contact-error').fadeOut('fast', function(){
                if(json.subjectMessage != '') {
                    $(this).html('(' + json.subjectMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-placement"] .contact-error').fadeOut('fast', function(){
                if(json.subjectMessage != '') {
                    $(this).html('(' + json.subjectMessage + ')').fadeIn('fast');
                }
            });
            $('.c-form-box form label[for="c-form-message"] .contact-error').fadeOut('fast', function(){
                if(json.messageMessage != '') {
                    $(this).html('(' + json.messageMessage + ')').fadeIn('fast');
                }
            });
            if(json.nameMessage == '' && json.emailMessage == '' && json.professionMessage == '' && json.subjectMessage == '' && json.messageMessage == '') {
                this_form_parent.find('.c-form-top').fadeOut('fast');
                this_form_parent.find('.c-form-bottom').fadeOut('fast', function() {
                    this_form_parent.append("<p>Thanks for contacting us! We will get back to you very soon.</p>");
                    // reload background
                    $('.c-form-container').backstretch("resize");
                });
            }
 
        }
    });
});
ts<?php

// Email address verification
function isEmail($email) {
	return filter_var($email, FILTER_VALIDATE_EMAIL);
}

if($_POST) {

    // Enter the email where you want to receive the message
    $emailTo = 'angeline.slayton@rocketmail.com';

    $name = addslashes(trim($_POST['name']));
    $clientEmail = addslashes(trim($_POST['email']));
    $phone = addslashes(trim($_POST['phone']));
    $artist = addslashes(trim($_POST['artist']));
    
    $tattoo = addslashes(trim($_POST['tattoo']));
    $size = addslashes(trim($_POST['size']));
    $budget = addslashes(trim($_POST['budget']));
    $place = addslashes(trim($_POST['place']));
    $message = addslashes(trim($_POST['message']));

    $array = array('nameMessage' => '', 
                    'emailMessage' => '', 
                    'phoneMessage' => '',
                    'artistMessage' => '',
                    'tattooMessage' => '', 
                    'sizeMessage' => '',
                    'budgetMessage' => '',
                    'placeMessage' => '',
    				 
    				'messageMessage' => ''
    			);

    if($name == '') {
    	$array['nameMessage'] = 'Empty name!';
    }
    if(!isEmail($clientEmail)) {
        $array['emailMessage'] = 'Invalid email!';
    }
    if($phone == 'Your phone...') {
    	$array['phoneMessage'] = 'Phone Number';
    }
    if($artist == 'Choose an artist...') {
    	$array['artistMessage'] = 'Choose an artist!';
    }
    if($tattoo == 'Choose a tattoo...') {
    	$array['tattooMessage'] = 'Choose a tattoo!';
    }
    if($size == 'Choose a size...') {
    	$array['sizeMessage'] = 'Choose a size!';
    }
    if($budget == 'Choose a budget...') {
    	$array['budgetMessage'] = 'Choose a budget!';
    }
    if($place == 'Choose a place...') {
    	$array['placeMessage'] = 'Choose a place!';
    }
    if($message == '') {
        $array['messageMessage'] = 'Empty message!';
    }
    
    if($name!='' && isEmail($clientEmail) && $phone!='Your phone...'&& $artist!='Choose an artist...'&& $tattoo!='Choose a tattoo...' && $size!='Choose a size...'&& $budget!='Choose a budget...'&& $place!='Choose a place...'&& $message!='') {
		// Send email
		$message = "Message from: " . $name . "\r\n" . "Profession: " . $profession . "\r\n" . $message;
		$headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
		mail($emailTo, $subject . " (contact form template dropdown)", $message, $headers);
    }

    echo json_encode($array);

}

?>

0 个答案:

没有答案