1

好的,所以我在谷歌搜索了一段时间后找不到这个,我试图在 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();
?>
4

4 回答 4

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;

于 2013-05-02T08:49:26.140 回答
2

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

例如:

发送电子邮件.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 */

  }

?>

表单电子邮件.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>
于 2013-05-02T08:46:46.630 回答
1

You could also use an automated redirect, instead of a button:

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

See it as a meta-redirect, which you would use in HTML. The refresh:5 indicates the interval is 5 seconds before it redirects, to the url specified as "previouspage.php".

This example isn't a button as stated in your question, but it should do the trick :) Also, in addition to this, you could add an extra echo telling your visitors that they will be redirected back automatically.

于 2013-05-02T08:45:11.950 回答
0

我最终为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 上接触到所有这些思想,就无法完成我想要的!

于 2013-05-06T05:28:56.970 回答