我没有找到答案,所以我问你们。
window.location.reload()不断重新加载而不会中断。

我正在尝试进行一些检查,以检查表单中是否没有输入,并且如果我不希望它发出警报,它正在工作,但是会不断重新加载。这是我的代码:

<?php
$from = $_POST['email_adress'];
$subject = $_POST['subject'];
$select = $_POST['select'];
$message2 = $_POST['message2'];
$name = $_POST['firstname'];
$name2 = $_POST['lastname'];
if ($_POST['firstname'] == "") {
    echo '<script language="javascript">';
    echo 'alert("First Name is Mandatory.");';
    echo 'window.location.reload("contactus.html");';
    echo '</script>';
    exit;

}
elseif  ($_POST['subject'] == "") {
    echo '<script language="javascript">';
    echo 'alert("Subject is Mandatory.");';
    echo 'window.location.reload("contactus.html");';
    echo '</script>';
    exit;
}
elseif ($_POST['email_adress'] == "") {
    echo '<script language="javascript">';
    echo 'alert("Email Adress is Mandatory.");';
    echo 'window.location.reload("contactus.html");';
    echo '</script>';
    exit;
}
elseif ($_POST['message2'] == "") {
    echo '<script language="javascript">';
    echo 'alert("A Message is Mandatory.");';
    echo 'window.location.reload("contactus.html");';
    exit;
    echo '</script>';
    exit;
} else {
header("Location: contactus.html");
$email_subject = "A submittion form";
$to ="[email protected]";
$headers = "From: [email protected]";
$email_body = 'You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the general subject of $select and more specifically $subject and the message is $message2';
mail($to,$email_subject,$email_body, $headers);
}

?>

最佳答案

也许这可以成为灵感?

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'GET') {

    $is_redirect = isset($_SESSION['to']);
    if ($is_redirect && !is_mail_valid($_SESSION)) {
        $to = $_SESSION['to'];
        $subject = $_SESSION['subject'];
        $body = $_SESSION['body'];
    }
    $_SESSION = array();
} else {
    if (is_mail_valid($_POST)) {
        $to = $_POST['to'];
        $subject = $_POST['subject'];
        $body = $_POST['body'];
        mail($to, $subject, $body);
    } else {
        $_SESSION = $_POST;
    }
    header("Location: index.php");
    exit;
}

function is_mail_valid($mail) {
    global $errors;
    global $valid;
    $errors = array();
    if (trim($mail['to']) == FALSE) { $errors['empty_to'] = TRUE; }
    if (trim($mail['subject']) == FALSE) { $errors['empty_subject'] = TRUE; }
    if (trim($mail['body']) == FALSE) { $errors['empty_body'] = TRUE; }
    $valid = empty($errors);
    return $valid;
}

?>



<?php if (!$valid) { ?>
<script type="text/javascript">
    <?php if ($errors['empty_to']) {?>
        alert('To: cannot be empty')
    <?php } ?>
    <?php elseif ($errors['empty_subject']) {?>
        alert('Subject: cannot be empty')
    <?php } ?>
    <?php elseif ($errors['empty_body']) {?>
        alert('Body: cannot be empty')
    <?php } ?>
</script>
<?php } ?>

<form method="post">
    <div>
        <label for="to">To</label>
        <input id="to" type="email" name="to" value="<?php echo $to;?>" />
    </div>
    <div>
        <label for="subject">Subject</label>
        <input id="subject" type="text" name="subject" value="<?php echo $subject;?>" />
    </div>
    <div>
        <label for="body">Body</label>
        <textarea id="body" name="body"><?php echo $body;?></textarea>
    </div>
    <div>
        <input type="submit" value="Send">
    </div>
</form>


基本上,您开始使用session_start()使用会话

在POST上:


如果没有错误:


发送邮件
重定向到conactactus页面。

如果有错误:


将输入保存在会话中
重定向到联系我们页面



在GET上:


如果会话中没有任何内容或会话中的邮件有效,则只需渲染html。
如果会话中有某些内容并且该数据无效,请呈现html,呈现警报脚本并设置输入的“值”属性以维护用户的输入值。
清除会话数据。

关于javascript - window.location.reload不断重新加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41200474/

10-09 14:47