Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用标头重定向PHP表单_Php_Html_Forms - Fatal编程技术网

使用标头重定向PHP表单

使用标头重定向PHP表单,php,html,forms,Php,Html,Forms,我基本上是想 验证表单 如果已验证,则重定向到成功页面response.php 我的表单是HTML。这只是一个带有粘性PHP的反馈表单 <form method="post" action=""> <fieldset> <legend>Contact Form</legend> <label>Name (Required)</label> &

我基本上是想

验证表单 如果已验证,则重定向到成功页面response.php 我的表单是HTML。这只是一个带有粘性PHP的反馈表单

        <form method="post" action="">

        <fieldset>
        <legend>Contact Form</legend>

        <label>Name (Required)</label>
        <input name="name" type="text" placeholder="Enter your name. (e.g. Arthur)" value="<?php if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']);
        ?>">
        <span class="err"><?php echo $nameErr;?></span>

        <label>Email (Required)</label>
        <input name="email" type="email" placeholder="Enter a valid e-mail. (e.g. someone@host.com)" value="<?php if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']);
        ?>">
        <span class="err"><?php echo $emailErr;?></span>


        <?php
            if (isset($_POST['reason']))
            {
                $reasonVar = $_POST['reason'];
            }
        ?>

        <label>Reason</label>
        <select name="reason">
          <option <?php if($reasonVar=="question") echo 'selected="selected"'; ?> value="question">Question</option>
          <option <?php if($reasonVar=="feedback") echo 'selected="selected"'; ?> value="feedback">Feedback</option>
          <option <?php if($reasonVar=="suggestions") echo 'selected="selected"'; ?> value="suggestions">Suggestions</option>
          <option <?php if($reasonVar=="other") echo 'selected="selected"'; ?> value="other">Other</option>
        </select>

        <label>Message (Required)</label>
        <textarea name="message" placeholder="Enter your message. (e.g. Your website is awesome!)" ><?php if(isset($_POST['message'])) echo htmlspecialchars($_POST['message']);?></textarea>
        <span class="err"><?php echo $messageErr;?></span>

        <input class="sub" name="submit" type="submit" value="Submit">

        </fieldset>

    </form>
下面是该页面的PHP。它只是在表单上执行基本验证,并尝试在所有验证成功后使用header函数将页面重定向到response.php,从而生成长if语句

        <?php
        $nameErr = $name = "";
        $emailErr = $email ="";
        $messageErr = $message ="";
        if ($_SERVER["REQUEST_METHOD"] == "POST"){

            if(empty(trim($_POST["name"]))){
                $nameErr="Missing";
            }
            else{
                $name = $_POST["name"];
            }

            if(empty(trim($_POST["email"]))){
                $emailErr = "Missing";
            }
            elseif(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
                $emailErr = "Invalid";
            }
            else{
                $email=$_POST["email"];
            }
            if(empty(trim($_POST["message"]))){
                $messageErr="Missing";
            }
            else{
                $message = $_POST["message"];
            }

            if(!empty(trim($_POST["name"]))&&!empty(trim($_POST["email"]))&&filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)&&!empty(trim($_POST["message"]))){
                header("Location:response.php");
                exit;
            }
        }

        ?>

现在,如果表单经过验证,表单就会消失,我会停留在表单页面上,而不会重定向到成功页面。我是一个非常新的后端网络编码,所以任何帮助将不胜感激。提前非常感谢

如果页面上已经有输出,那么在打开标题重定向后立即将错误报告添加到文件顶部将不起作用,也许您可以将php代码移到html之前?嘿,大家好,谢谢回来!我使用了@Fred ii的错误报告,它说由于包含php函数,标题已经发送出去了。我将此包含用于导航栏。我该怎么办?不客气。读这篇文章,里面有很多信息。