【问题标题】:Back button in phpphp中的后退按钮
【发布时间】:2013-04-26 08:49:46
【问题描述】:

好的,所以我在谷歌搜索了一段时间后找不到这个,我试图在 php 的错误页面上有一个后退按钮,现在它会说“电子邮件错误”“名字错了”请回去再试一次,但我希望能够在那里有一个按钮,因为我在这些表单的页面上有多个 iFrame,我尝试过的所有内容都给我 php 错误,我不知道还能做什么!

<?php
if(isset($_POST['email'])) {



    function died($error) {
        echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name'];
    $email_to = $_POST['email'];
    $comments = $_POST['comments'];
    $email_from = $_POST['emailf'];
    $email_subject = $_POST['emailt'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Dear ".clean_string($first_name);
    $email_message .= ",\n\n".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>


Your Email has been sent, please verify!
<form><input type="button" value="Back" onClick="history.back();return true;"></form>

<?php
}
die();
?>

【问题讨论】:

  • 您遇到了哪些错误?当您遇到特定错误时,代码是什么样的?
  • 小事用if(!isset($_POST['first_name'], $_POST['email'], $_POST['comments'], $_POST['emailf'], $_POST['emailt'])){

标签: php button back


【解决方案1】:

您也可以使用自动重定向,而不是按钮:

header('refresh:5;url=previouspage.php');

将其视为一种元重定向,您将在 HTML 中使用它。 refresh:5 表示在重定向之前的间隔是 5 秒,到指定为“previouspage.php”的 url。

此示例不是您问题中所述的按钮,但它应该可以解决问题:) 此外,除此之外,您还可以添加一个额外的回声,告诉您的访问者他们将被自动重定向回来。

【讨论】:

  • 很确定这行不通。要更改位置,您需要提供标头函数Location: previouspage.php
  • 确实,通常情况下,你会写header('location:previouspage.php');。但是,我用谷歌搜索了它,因为不久前我在自己的项目中使用了它。工作得很好。
【解决方案2】:

这里的方法是将每个输入存储在一个会话中,这样当用户返回输入时它仍然存在。然后你可以通过 A 标签将它们发送回来,如果你想让它看起来像一个按钮,只需使用 css 来设置它的样式。

例如:

SendEmail.php

<?php
  session_start();

  $foo1 = $_POST['foo1'];
  $foo2 = $_POST['foo2']

  if($error) {
    /* Put inputs in an session */
    $_SESSION['foo1'] = $foo1;
    $_SESSION['foo2'] = $foo2;

    echo 'error <a href="FormEmail.php">go back</a>';
  } else {

    /* Everything is okay, so we can trough our sessions away. */
    unset($_SESSION['foo1']);
    unset($_SESSION['foo2']);

    /* Send email */

  }

?>

FormEmail.php

<?php
  session_start();
?>

<form>
  <input type="input" name="foo1" value="<?php echo $_SESSION['foo1']; ?>" />
  <input type="input" name="foo2" value="<?php echo $_SESSION['foo2']; ?>" />
  <input type="submit" name="submit" value="submit" />
</form>

【讨论】:

    【解决方案3】:

    你试过用这样的东西吗?

    header('Location: previouspage.php');
    exit;
    

    exit 很重要,因此您不要在 PHP 中抛出“标头已发送”异常。 Exit 将停止脚本在原处执行。

    编辑

    稍微误读了问题,所以您想在显示返回按钮的同时显示错误消息?稍微移动一下代码:

    <?php
    if(isset($_POST['email'])) {
    
    
    
        function died($error) {
            echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }
    
        if(!isset($_POST['first_name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['comments'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }
    
        $first_name = $_POST['first_name'];
        $email_to = $_POST['email'];
        $comments = $_POST['comments'];
        $email_from = $_POST['emailf'];
        $email_subject = $_POST['emailt'];
    
        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
        $string_exp = "/^[A-Za-z .'-]+$/";
      if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
        echo $error_message;
        echo '<form><input type="button" value="Back" onClick="history.back();return true;"></form>';
        exit;
      }
    
        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }
    
        $email_message .= "Dear ".clean_string($first_name);
        $email_message .= ",\n\n".clean_string($comments)."\n";
    
    
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
    ?>
    
    
    Your Email has been sent, please verify!
    <form><input type="button" value="Back" onClick="history.back();return true;"></form>
    
    <?php
    }
    die();
    ?>
    

    现在,当它显示错误消息时,我们不再调用 dead 方法,而是回显消息,回显您在下方使用的相同后退按钮,然后使用 exit; 终止脚本

    【讨论】:

      【解决方案4】:

      我最终为 php 做了这个

      <?php
      if(isset($_POST['email'])) {
      
      
      
          function died($error) {
              echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
              echo $error."<br /><br />";
              echo "Please go back and fix these errors.<br /><br />";
              die();
          }
      
          if(!isset($_POST['first_name']) ||
              !isset($_POST['email']) ||
              !isset($_POST['comments'])) {
              died('We are sorry, but there appears to be a problem with the form you submitted.');       
          }
      
          $first_name = $_POST['first_name'];
          $email_to = $_POST['email'];
          $comments = $_POST['comments'];
          $email_from = $_POST['emailf'];
          $email_subject = $_POST['emailt'];
      
          $error_message = "";
          $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
        if(!preg_match($email_exp,$email_to)) {
          $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }
          $string_exp = "/^[A-Za-z .'-]+$/";
        if(!preg_match($string_exp,$first_name)) {
          $error_message .= 'The First Name you entered does not appear to be valid.<br />';
        }
        if(strlen($error_message) > 0) {
          echo $error_message;
          echo '<form><input type="button" value="Back" onClick="history.back();return true;"></form>';
          exit;
        }
      
          function clean_string($string) {
            $bad = array("content-type","bcc:","to:","cc:","href");
            return str_replace($bad,"",$string);
          }
      
          $email_message .= "Dear ".clean_string($first_name);
          $email_message .= ",\n\n".clean_string($comments)."\n";
      
      
      $headers = 'From: '.$email_from."\r\n".
      'Reply-To: '.$email_from."\r\n" .
      'X-Mailer: PHP/' . phpversion();
      @mail($email_to, $email_subject, $email_message, $headers);  
      ?>
      <?php
      {
      echo '<script type="text/javascript">history.go(-1);</script>';
      }
      ?>
      <?php
      die();
      }
      ?>
      

      这是 HTML

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-type" content="text/html; charset=utf-8">
      <meta name="author" content="Oscar Oliu">
      <title>Tech Email Template 1<</title>
      <style type="text/css">
      h4
      {
      text-align:center;
      }
      form
      {
      text-align:center;
      }
      textarea.c1
      {
      display:none;
      }
      p
      {
      text-align:center;
      }
      </style>
      </head>
      <body onLoad="document.forms.htmlform.reset()">
      <h4>Tech Email Template 1</h4>
      <form id="htmlform" name="htmlform" method="post" action="html_form_send.php">
      First Name:<br><input type="text" name="first_name" maxlength="50" size="25"><br>
      Email Address:<br><input type="text" name="email" maxlength="80" size="25"><br>
      <textarea class="c1" cols="0" rows="0" name="emailf">xxxx@xxxx.com</textarea>
      <textarea class="c1" name="emailt" cols="0" rows="0">xxx</textarea>
      <input type="submit" value="Submit">
      <textarea class="c1" name="comments" cols="32" rows="8"> Email Test Template</textarea>
      <textarea readonly cols="32" rows="8">Email Test Template</textarea>
      </form>
      <p>This form will automatically reload on use.</p>
      </body>
      </html>
      

      再次感谢大家的帮助,如果没有接触到 StackOverflow 的所有这些思想,就无法完成我想要的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-28
        • 2015-12-20
        • 2013-01-09
        • 1970-01-01
        • 2011-11-22
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        相关资源
        最近更新 更多