dongru2019 2014-04-11 14:56
浏览 57
已采纳

表单提交后的PHP消失,而不是通过标头位置重定向

Cannot redirect form after submission. Form simply disappears from page and the page stays the same.

Any input is welcomed. Using Twitter Bootstrap, GoDaddy hosting (I know).

FORM:

<div class="row">
    <div class="col-lg-7 col-lg-offset-3">
        <form class="well" role="form" id="colform" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
            <div class="row">
                <div class="col-lg-3 pull-left error">
                    <p class="text-left">* - Required Fields</p>
                </div>
            </div>
            <div class="row">
                <div class="form-group col-sm-4">
                    <label class="control-label" for="name">Your Name *</label>
                    <input type="text" class="form-control" name="name">
                    <span class="help-block error"><?php echo $nameErr;?></span>
                </div>
                <div class="form-group col-sm-4">
                    <label class="control-label" for="phone_number">Phone Number *</label>
                    <input type="text" class="form-control" name="phone_number">
                    <span class="help-block error"><?php echo $phone_numberErr;?></span>
                </div>
                <div class="form-group col-sm-4">
                    <label class="control-label" for="email">Email</label>
                    <input type="text" class="form-control" name="email">
                    <span class="help-block error"><?php echo $emailErr;?></span>
                </div>
            </div>

            <div class="row">
                <div class="form-group col-sm-4">
                    <label class="control-label" for="city">City *</label>
                    <input type="text" class="form-control" name="city">
                    <span class="help-block error"><?php echo $cityErr;?></span>
                </div>
                <div class="form-group col-sm-4">
                    <label class="control-label" for="post_code">Post Code *</label>
                    <input type="text" class="form-control" name="post_code">
                    <span class="help-block error"><?php echo $post_codeErr;?></span>
                </div>
                <div class="form-group col-sm-4">
                    <label class="control-label" for="full_address">Full Address *</label>
                    <input type="text" class="form-control" name="full_address">
                    <span class="help-block error"><?php echo $full_addressErr;?></span>
                </div>
            </div>

            <div class="form-group row">
                <div class="input-group col-md-6 col-md-offset-3">
                    <span class="input-group-addon">Quantity of bags you have: *</span>
                    <input type="text" class="form-control" name="bags">
                </div>
            </div>

            <div class="row form-group">
                <div class="col-sm-10 col-sm-offset-1">
                    <label class="" >&nbsp; &nbsp;Please leave any additional information you would think is important to mention:</label>
                    <textarea class="form-control" rows="3" name="comments"></textarea>
                </div>
            </div>

            <div class="row form-group">
                <div class="col-sm-10 col-sm-offset-1">
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="updates"> If you would like to receive news and updates on our services, please thick this box
                        </label>
                    </div>
                </div>
            </div>

            <div class="alert alert-danger"><span class="label label-danger">IMPORTANT !</span> &nbsp; &nbsp;
                Each item in the bag is checked, please make sure that it doesn't contain following items: </br></br>
                <div class="text-center"><strong>wet, dirty, ripped, bad quality or old fashioned clothing(90s, 80s or older).</br>
                    underwear, school uniform, work clothes.</strong></div></br>

                <div class="text-center small">It will save your precious time and prevent any inconvenience that might occur during the process, which we always try to make no longer than 10-12mins no matter the quantity.</br>
                    Can't check everything or not sure what's inside the bags?</br> Not a problem at all, just make a note of that in the comment box above.
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6 col-sm-offset-3">
                    <button type="submit" class="btn btn-primary btn-lg btn-block" name="fsubmit" >Send Request</button>
                </div>
            </div>

        </form>
    </div>
</div>

PHP CODE IS JUST ABOVE THIS HTML:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $cityErr = $post_codeErr = $full_addressErr = $phone_numberErr = "";
$name = $email = $city = $comment = $post_code = $full_address = $phone_number = $bags = "";
$nameCheck = $cityCheck = $post_codeCheck = $full_addressCheck = $phone_numberCheck = $emailCheck = false;

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    /*  CHEKING NAME FIELD  */
    if (empty($_POST["name"]))
       { $nameErr = "Name is required"; }
    else {
          $name = test_input($_POST["name"]);
          if (!preg_match("/^[a-zA-Z ]*$/",$name))
             { $nameErr = "Only letters and white space allowed"; }
          else
               { $nameCheck=true; }
         }

    /*  CHECKING CITY FIELD */
    if (empty($_POST["city"]))
       { $cityErr = "City is required"; }
    else {
          $city = test_input($_POST["city"]);
          if (!preg_match("/^[a-zA-Z ]*$/",$city))
             { $cityErr = "Only letters and white space allowed"; }
          else
               { $cityCheck=true; }
         }

    /* CHECKING POST CODE FIELD*/
    if (empty($_POST["post_code"]))
       { $post_codeErr = "Post Code is required"; }
    else {
          $post_code = test_input($_POST["post_code"]);
          if (!preg_match("/^[a-zA-Z \d]*$/",$post_code))
             { $post_codeErr = "Only letters, whole numbers and white space allowed"; }
          else
               { $post_codeCheck=true; }
         }

    /* CHECKING FULL ADDRESS FIELD */
    if (empty($_POST["full_address"]))
       { $full_addressErr = "Full address is required"; }
    else {
          $full_address = test_input($_POST["full_address"]);
          if (!preg_match("/^[a-zA-Z \d,.]*$/",$full_address))
             { $full_addressErr = "Only letters, whole numbers, commas, dot and white space allowed"; }
          else
               { $full_addressCheck=true; }
         }

    /* CHECKING PHONE FIELD */
    if (empty($_POST["phone_number"]))
       { $phone_numberErr = "Phone number is required"; }
    else {
          $phone_number = test_input($_POST["phone_number"]);
          if (!preg_match("/^[ \d]*$/",$phone_number))
             { $phone_numberErr = "Only whole numbers and white space allowed"; }
          else
               { $phone_numberCheck=true; }
         } 

    /* CHECKING BAG FIELD */
    if (empty($_POST["bags"]))
       { $bags = ""; }
    else
         { $bags = test_input($_POST["bags"]); }

     /* CHECKING EMAIL FIELD */
     if (empty($_POST["email"]))
        { $email = "No email entered";
          $emailCheck=true; }
     else {
           $email = test_input($_POST["email"]);
           if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
              { $emailErr = "Invalid email format"; }
           else
                { $emailCheck=true; }
           }

    /* CHECKING COMMENTS FIELD */
    if (empty($_POST["comment"]))
       { $comment = ""; }
    else
         { $comment = test_input($_POST["comment"]); }

    if ($nameCheck && $cityCheck && $post_codeCheck && $full_addressCheck && $phone_numberCheck && $emailCheck)
       { $email = $_POST['email'] ;
         $subject = date("d.M H:i") ;
         $message = "Name:  " . $_POST['name'] . "
" . "City:  " . $_POST['city'] . "
" . "Post Code:  " . $_POST['post_code'] . "
" . "Full Address: " . $_POST['full_address'] . "
" . "Phone Number: " . $_POST["phone_number"] . "
" . "Email: " . $_POST["email"] . "
" . "Number of bags: " . $_POST["bags"] . "
" . "Comments: " . $_POST["comments"] . "
" . "Would like to receive updates: " . $_POST['updates'];
         mail("collections@c4c-essex.co.uk", "$subject", $message, "From:" . $email); /* Send Email */
         header("Location: http://www.google.com");
         exit();
        }
}

function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}               

?>

  • 写回答

1条回答 默认 最新

  • dongluedeng1524 2014-04-11 15:14
    关注

    You need to use header in the top of your code without any output on page.

    for example if you use a code like this it's not work correctly:

    echo "hi";
    header("location: http://www.google.com");
    

    you can't output any data before header command.

    you should try to change your code positions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题