Si8
Si8

Reputation: 9225

Variable not getting displayed when printed on another page in PHP

<?php
session_start();

 error_reporting(E_ALL);
// Clean up the input values 
foreach($_POST as $key => $value) {  
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
}

$name = trim(strip_tags(stripslashes($_POST['name'])));
$email = trim(strip_tags(stripslashes($_POST['email'])));
$subject = trim(strip_tags(stripslashes($_POST['subject'])));
$message = trim(strip_tags(stripslashes($_POST['message'])));

$successful = "Message was sent";
$failure = "Message was not sent";

$header = "From: $email\r\n";

$validEmail = filter_var($email, FILTER_VALIDATE_EMAIL);

if (isset($name) && isset($email) && isset($subject) && isset($message)) {
    if ($validEmail) {
        mail ("[email protected]", $subject, $message, $header);
        header("Location: emailsent.php?message=$successful");
    }
    else {
        header("Location: emailnotsent.php?message=$failure");
    }
}
?>

emailnotsent.php:

<?php
session_start();

echo $failure;
?>

emailsent.php:

<?php
session_start();

echo $successful;
?>

I see the URL with the "Message was sent" but nothing is displayed on the page itself.

Upvotes: 0

Views: 93

Answers (2)

user4035
user4035

Reputation: 23719

Nothing is displayed, because $successful is not defined in emailsent.php. Try to print the GET variable:

echo $_GET['message'];

See Variable Scope.

Upvotes: 4

Maximus2012
Maximus2012

Reputation: 1819

emailnotsent.php

<?php
session_start();

echo $_GET['message'];
?>

emailsent.php

<?php
session_start();

echo $_GET['message'];
?>

Upvotes: 1

Related Questions