dse55384 2016-12-17 16:05
浏览 555
已采纳

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);
}

?>
  • 写回答

2条回答 默认 最新

  • 普通网友 2016-12-17 22:01
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog