window.location.reload不断重新加载

I didn't find an answer so im asking you guys. window.location.reload() constantly reloads without a break.

I'm trying to make something that checks if the form has no input in it, and if doesn't I want it to make an alert, it's working but it reloads constantly. Here's my code:

<?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 ="sudaiguy1@gmail.com";
$headers = "From: sudaiguy1@gmail.com";
$email_body = 'You have been contacted by $name $name2 and his email is $from. 
 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);
}

?>

Maybe this can be an inspiration?

<?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>

Basically you start using session using session_start()

On POST:

  • If you don't have an error:
    • send the mail
    • redirect to the conctactus page.
  • If you have an error:
    • save the input in the session
    • redirect to the contact us page

On GET:

  • If you have nothing in session or the mail in session is valid, just render the html.
  • If you have something in session and that data is invalid, render the html, render the alert script, and set the "value" attributes of the input to maintain the user's input values.
  • Clear the session data.

This solution requires that you change your contactus.html to contactus.php in order to use sessions.

<?php
// to use a session, you must start it
session_start();
// variable to store any error message
$error = null;

$from = $_POST['email_adress'];
$subject = $_POST['subject'];
$select = $_POST['select'];
$message2 = $_POST['message2'];
$name = $_POST['firstname'];
$name2 = $_POST['lastname'];

if ($_POST['firstname'] == "") {
    $error = "First Name is Mandatory.";
} elseif  ($_POST['subject'] == "") {
    $error = "Subject is Mandatory.";
} elseif ($_POST['email_adress'] == "") {
    $error = "Email Adress is Mandatory.";
} elseif ($_POST['message2'] == "") {
    $error = "A Message is Mandatory.";
}

if ($error) {
    // store the error in a session so that you can use it on another page
    $_SESSION['error'] = $error;
} else {
    $email_subject = "A submittion form";
    $to ="sudaiguy1@gmail.com";
    $headers = "From: sudaiguy1@gmail.com";
    $email_body = 'You have been contacted by $name $name2 and his email is $from. 
 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);
}

// regardless of whether there is an error or not, it always goes to Contact Us page
header("Location: contactus.php");
exit;
?>

Then in your contactus.php, you would have

<?php
// resume session again
session_start();
?>

<!-- somewhere in your HTML code -->

<?php if (isset($_SESSION['error'])) : ?>
    <div class="error-message"><?php echo $_SESSION['error'] ?></div>
<?php endif ?>

<?php
// you probably will want to remove the error afterwards
unset($_SESSION['error']);
?>

If you can't do that, then this solution will not work and you'll have to resort to AJAX (where you send back your error message via the response you get back from your AJAX response).