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 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析