PHP联系表单die()函数替代?

时间:2016-01-27 04:09:43

标签: php contact-form die

我有一个联系表格,但每次有人提交时,我们都会将其发送到一个新的空白页面,其中包含“已发送邮件成功”或带有“X或Y或Z错误”的页面。

我希望在用户所在的同一页面上弹出错误或通知。我怎么能这样做?

谢谢。 这是代码。

// Clean up the input values
foreach($_POST as $key => $value) {
    if(ini_get('magic_quotes_gpc'))
        $_POST[$key] = stripslashes($_POST[$key]);

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

// Check input values for errors
$errors = array();
if(strlen($name) < 2) {
    if(!$name) {
        $errors[] = "Please enter your name!";
    } else {
        $errors[] = "Name requires at least 2 characters!";
    }
}
if(!$email) {
    $errors[] = "Please enter your email!";
} else if(!validEmail($email)) {
    $errors[] = "Please enter a valid email!";
}
if(strlen($message) < 1) {
    if(!$message) {
        $errors[] = "Please enter your Instagram username!";
    } else {
        $errors[] = "Please enter your Instagram username!";
    }
}

// Output error message(s)
if($errors) {
    $errortext = "";
    foreach($errors as $error) {
        $errortext .= "<li>".$error."</li>";
    }
    die("<ul class='errors arrowed'>". $errortext ."</ul>
        <a href='index.html' class='btn'><i class='icon-left-1'></i> Back</a>");
}

// Send the email
if($subject!=""){
    $subject = "Contact Form: $subject";
}
else {
    $subject = "Contact Form: $name";
}
$message = "$message";
$headers = "From: ".$name." <".$email.">" . "\r\n" . "Reply-To: " . $email;

mail($to, $subject, $message, $headers);

// Output success message
die("<p class='success'>Thank you! – Your message has been successfully sent!</p>");

// Check if email is valid
function validEmail($email) {
    $isValid = true;
    $atIndex = strrpos($email, "@");
    if (is_bool($atIndex) && !$atIndex) {
        $isValid = false;
    }
    else {
        $domain = substr($email, $atIndex+1);
        $local = substr($email, 0, $atIndex);
        $localLen = strlen($local);
        $domainLen = strlen($domain);
        if ($localLen < 1 || $localLen > 64) {
            // Local part length exceeded
            $isValid = false;
        }
        else if ($domainLen < 1 || $domainLen > 255) {
            // Domain part length exceeded
            $isValid = false;
        }
        else if ($local[0] == '.' || $local[$localLen-1] == '.') {
            // Local part starts or ends with '.'
            $isValid = false;
        }
        else if (preg_match('/\\.\\./', $local)) {
            // Local part has two consecutive dots
            $isValid = false;
        }
        else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
            // Character not valid in domain part
            $isValid = false;
        }
        else if (preg_match('/\\.\\./', $domain)) {
            // Domain part has two consecutive dots
            $isValid = false;
        }
        else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
        str_replace("\\\\","",$local))) {
            // Character not valid in local part unless local part is quoted
            if (!preg_match('/^"(\\\\"|[^"])+"$/',
            str_replace("\\\\","",$local))) {
                $isValid = false;
            }
        }
        if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
            // Domain not found in DNS
            $isValid = false;
        }
    }
    return $isValid;
}

&GT;

0 个答案:

没有答案