Silentwidow
Silentwidow

Reputation: 15

Back button in php

Ok, so I was not able to find this after searching through google for a while, I'm trying to have a back button on the error page for php, right now it will say "email is wrong" "first name is wrong" please go back and try again, but I want to be able to have a button there, since I have multiple iFrames on a page for these forms, everything I've tried just gives me php errors, I don't know what else to do!

<?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();
?>

Upvotes: 1

Views: 8510

Answers (4)

Silentwidow
Silentwidow

Reputation: 15

I ended up doing this for the 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();
}
?>

And this for the 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">[email protected]</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>

Thank you all again for all your help, couldn't have done what exactly I wanted without having access to all these minds at StackOverflow!

Upvotes: 0

S.Visser
S.Visser

Reputation: 4725

The way to go here is to store each input in an session so when the user goes back the input it still remains. Then you can just send them back via an A tag, if you want it to look like a button just use css to style it.

For Example:

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>

Upvotes: 2

Turnerj
Turnerj

Reputation: 4278

Have you tried using something like this?

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

The exit is important so you don't throw a "headers already sent" exception in PHP. Exit will stop the script executing right where it is.

EDIT

Misread the question slightly, so you want to display your error messages while displaying that back button? Just move the code around a bit:

<?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();
?>

Now when it displays the error messages, rather than calling the died method, we just echo out the message, echo out the same back button you used further down and kill the script with exit;

Upvotes: 3

Edwin Lambregts
Edwin Lambregts

Reputation: 408

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.

Upvotes: 1

Related Questions