-1

I have a contact.html page I have a form on. The form action goes to .php page to handle the email, nothing special. On that page I have:

<?php
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

$FirstName = check_input($_REQUEST['FirstName']);
$LastName = check_input($_REQUEST['LastName']);
$email = check_input($_REQUEST['email']);
$phone = check_input($_REQUEST['phone']);
$message = check_input($_REQUEST['message']);
$human = check_input($_REQUEST['human']);
$webpackage = check_input($_REQUEST['webpackage']);
$webdesign = check_input($_REQUEST['webdesign']);
$customdesign = check_input($_REQUEST['customdesign']);

if ($human == 5) {

$to = "****.com";
$subject = "From ****";

$body = " From: $FirstName  $LastName\n\n E-Mail: $email\n\n Phone:     $phone\n\n Message:\n\n $message\n\n Web Package:$webpackage\n\n Web     Design:$webdesign\n\n Custom Design:$customdesign";


mail ($to, $subject, $body);
header('location: index.html');
}
else {
$result="<div class=\"alert alert-danger\">Sorry there was an error sending     your message. Please go back and check your anti-spam answer</div>";
}

?>

I have a simple box that equals 5 that I am checking value for. This works and email sent with all info. BUT if not equal to 5 is where the problem starts. The page goes to my action.php page and is blank.

My html on the contact.html page is:

    <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                    <?php echo($result); ?>
                </div>
            </div>

Using this to get to my action.php page through form. Everything else is .html:

 <form class="form-horizontal" id="contact-form" method="post" action="/action.php">

Is there a way to do this? I have a work around where I just echo the error from the .php page. This works if !=5 but not exactly what I want to do. As you may tell, I am not PHP literate.

rubinmon
  • 153
  • 3
  • 12
Steve V.
  • 29
  • 7
  • Maybe some of the code you left out is getting an error and the script stops. – Barmar Sep 02 '16 at 02:02
  • What do you mean by "The page goes to my .php page"? How are you going from the page that handles the email to the page that displays the DIV? – Barmar Sep 02 '16 at 02:04
  • I think you need to post the whole script, it's impossible to tell what you're really doing from the snippets you've given. – Barmar Sep 02 '16 at 02:05
  • Hopefully edited to help @Barmar. My error, I assume everyone knows what I am thinking sometimes. Posting form to action.php above. Works great when correct answer of 5 is given. Its the else that will not work. – Steve V. Sep 02 '16 at 13:23
  • `My html on the contact.html page is:` So.... why does your html page contain PHP code? – Xorifelse Sep 02 '16 at 14:13
  • Change your page name to `contact.php`. It will now be able to execute embedded PHP code. With the `.html` extension, your web server will treat it as a static page and not execute any embedded code. – Simba Sep 02 '16 at 14:15
  • I still don't understand how the PHP that processes the form is related to the HTML later. Variables don't persist between different pages, except for session variables in `$_SESSION`. So unless they're the same page, `$result` will be empty. – Barmar Sep 02 '16 at 15:26
  • Steve your flow of events is not clear. Assuming user enters 5 or whatever, in the input and submits, the page has to go to action.php (which seems to be in the root...due to the /). After that there doesn't seem to be a connection to the HTML you have posted.... like maybe a header redirect. Maybe you want to do a header redirect back to contact page if the $human is not 5 (if the result div is in contact page) – user3526204 Sep 09 '16 at 09:44

3 Answers3

0

You can set a variable in the $_SESSION[] array, and in your "else" section use Header() to redirect to a page where you display the value you stored.

See example in this other answered question:

displaying a message after redirecting the user to another web page

Community
  • 1
  • 1
akubot
  • 141
  • 1
  • 8
0
  1. Update your else part with following code :

     } else {
         header('location: contact.html?status=error');
     }
    
  2. Now check if get method is set on your contact.html page. if yes than set and display your $result value.

    <?php 
      if(isset($_GET['status']) && $_GET['status']=='error' ) { 
    
      $result="<div class=\"alert alert-danger\">Sorry there was an error sending     your message. Please go back and check your anti-spam answer</div>";           
    
    } ?>
    
  3. on contact.html check if $result has value and print it :)

Ravi Roshan
  • 570
  • 3
  • 14
0

Add a redirect towards contact.html in your action.php like this

else {
$result="Sorry there was an error sending your message. Please go back and check your anti-spam answer"; 
$result=str_replace(" ","%20",$result);
header('location: contact.html?result=$result'); 
} 

And then get the result in contact.html with GET

$result= $_GET['result'];

Ideally do the html mark up for result in the destination Contact.html page after you receive the result. That eliminates the nuances of passing html over http

user3526204
  • 509
  • 5
  • 22