douxia1988 2013-07-24 15:53
浏览 86
已采纳

自我提交表单重定向

I have a php from that includes the external process.php, which handles all my validation and sending the mail function and submits to it self by setting
action="<?php echo $_SERVER['PHP_SELF'] ?>"
Everything works great, it validates properly and mails the form to my email, but when I try to include the header("location: thanks.html") to redirect after submit, it doesn't redirect. Am I doing this incorrectly or is it an issue with self-submitting the form? I'm also using Jquery and Jquery Mobile. Any help is appreciated

Heres my php

<?php 
if(($_SERVER['REQUEST_METHOD'] =='POST') && (!empty ($_POST['action']))):

if (isset ($_POST['myname'])){$myname=$_POST['myname'];}
if (isset ($_POST['myphone'])){$myphone=$_POST['myphone'];}
if (isset ($_POST['myemail'])){$myemail=$_POST['myemail'];}
if (isset ($_POST['job'])){$job=$_POST['job'];}
if (isset ($_POST['comments'])){
    $comments= filter_var($_POST['comments'], FILTER_SANITIZE_STRING);}

$formerrors = false;



      if($myname === '') :
      $err_myname = '<div class="error"> Sorry, your name is rquired</div>';
      $formerrors=true;
      endif;    

      if($myphone === ''):
      $err_myphone = '<div class="error"> Sorry, your phone number is rquired</div>';
      $formerrors=true;
      endif;    

      if($myemail === ''):
      $err_myemaile =  '<div class="error"> Sorry, your email is rquired</div>';
      $formerrors=true;
      endif;    

      if($job === ''):
      $err_job =  '<div class="error"> Sorry, your business is rquired</div>';
      $formerrors=true;
      endif;    



if (!($formerrors)) :
$to = "myemail@emial.com";
$subject = " Request from $myname --Show and Tell";

$message = "A new show and tell request from:

            $myname 

            $myemail

            $myphone

            $job

            $comments
";
$replyto = "From: $myemail";


if(mail($to, $subject, $message)):
    $msg ="Thanks for filling out our form";
    header('location: http://localhost/thanks.html');
    else:
    $msg = "There was a problem sending the message";
    endif; //mail data

endif; //check errors

endif; //form submitted

?>

And my html

<div data-role="content">
<?php if (isset($msg)) { echo '<div id="formmessage"><p>', $msg, '</p> </div>';}?>
<form id="form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
  <li data-role="fieldcontain">
    <label for="myname">Your Name*:</label><br>
    <input type="text" name="myname" id="myname" placeholder="John Smith" value="<?php if (isset($myname)) {echo $myname;}  ?>" required/>
    <?php  if (isset($err_myname)) {echo $err_myname;} ?>
  </li>

    <li data-role="fieldcontain">
    <label for="myemail">Email*:</label><br>
    <input type="email" name="myemail" id="myemail" placeholder="jsmith@email.com" value="<?php if (isset($myemail)) {echo $myemail;}  ?>"  />
    <?php  if (isset($err_myemail)) {echo $err_myemail;} ?>
    <?php  if (isset($err_wrong)) {echo $err_wrong;} ?>
  </li>

    <li data-role="fieldcontain">
    <label for="myphone">Phone number*:</label><br>
    <input onblur="formatPhone(this);" type="tel" name="myphone" id="myphone" placeholder="1234567890"   value="<?php if (isset($myphone)) {echo $myphone;}  ?>"  required/>
    <?php  if (isset($err_myphone)) {echo $err_myphone;} ?>
  </li>

      <li data-role="fieldcontain">
    <label for="job">Name of Business*:</label><br>
    <input  type="text" name="job" id="job" placeholder="Where do you work?"   value="<?php if (isset($job)) {echo $job;}  ?>"  required/>
    <?php  if (isset($err_job)) {echo $err_job;} ?>
  </li>


    <li data-role="fieldcontain">
      <label for="comments">Comments:</label><br>
      <textarea cols="40" rows="8" name="comments" id="comments" placeholder="Questions or Comments?">
      <?php if (isset($comments)) {echo $comments;}?>
      </textarea>

    </li>

<div data-inline="true" data-type="horizontal">
<input name="Reset" type="reset" value="Reset" data-role="button" data-inline="true" />

<input type="submit" value="Submit" data-role="button" 
data-inline="true" name="action" />


</div>


</form>



</div>
  • 写回答

1条回答 默认 最新

  • doutang9037 2013-07-24 15:58
    关注

    1/ Simple test, are you sure you don't have a whitespace or any other character sent before the header()

    2/ Did you check you got the http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering option on ? (Else you have to explicitly start output buffering)

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

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?