window.location.reload不断重新加载

时间:2016-12-17 16:05:25

标签: javascript php

我没有找到答案所以我问你们。 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 ="sudaiguy1@gmail.com";
$headers = "From: sudaiguy1@gmail.com";
$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);
}

?>

2 个答案:

答案 0 :(得分:1)

此解决方案要求您将contactus.html更改为contactus.php才能使用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. \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);
}

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

然后在你的contactus.php中,你会有

<?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']);
?>

如果你不能这样做,那么这个解决方案将不起作用,你将不得不求助于AJAX(你通过你从AJAX响应中得到的响应发回错误信息)。

答案 1 :(得分:1)

也许这可能是一种灵感?

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

  • 如果您没有错误:
    • 发送邮件
    • 重定向到conctactus页面。
  • 如果您有错误:
    • 将输入保存在会话中
    • 重定向到联系我们页面

在GET上:

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