0

I have two files:

The first one is a php file called index.php - which is visible to the user and contains an HTML form.

<?php
ob_start();
session_start();
?>
<form method="post" action="create_profile.php">
....
<input id="personal_email" name="personal_email" tabindex="auto" type="text" />
....
<input name="submit" type="submit" value="Submit">
</form>
<?php
//Get value from create_profile.php
if(empty($_POST) === false) {
$invalidEmail = $_SESSION['invalidEmail'];
echo $invalidEmail;
}
?>

The second file create_profile.php process the above file index.php, basically it checks the form and sees if there are any errors - if there are any errors it is suppose to display errors in index.php

<?php
....
if (filter_var($personal_email, FILTER_VALIDATE_EMAIL)) {
$invalidEmail = 'The email address is invalid or missing' . '<br/>';
}
...
//Send errors to index.php
$_SESSION['invalidEmail'] = $invalidEmail;
header('Location: create_profile.php');
..
?>

The problem is that when I submit the form from index.php to create_profile.php - the error from create_profile.php is not being display in index.php

I am not getting any errors in my error log!

user3173207
  • 269
  • 1
  • 7
  • 21
  • Have you got **`session_start()`** at the top of your `create_profile.php` file? – Darren Feb 02 '14 at 00:28
  • http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php/12026863#12026863 – PeeHaa Feb 02 '14 at 00:29
  • What is not working - the redirection, or the displaying of the error? Have you debugged this, checking whether invalidEmail is ever set, the header redirect is ever executed, and the `if` condition in your first code ever triggered? – Pekka Feb 02 '14 at 00:41
  • I updated [**my answer below**](http://stackoverflow.com/a/21505781/) - I believe that is what you're looking to achieve. @user3173207 – Funk Forty Niner Feb 02 '14 at 01:41
  • You're welcome @user3173207 - Sessions can be tricky. – Funk Forty Niner Feb 02 '14 at 13:47

1 Answers1

1

Try this logic, which will display the invalid Email upon returning to index.php which I presume is the intended goal.

index.php

<?php
ob_start();
session_start();

if(isset($_SESSION['invalidEmail'])) {
$invalidEmail = $_SESSION['invalidEmail'];

echo $invalidEmail;
}

?>
<form method="post" action="create_profile.php">

<input id="personal_email" name="personal_email" tabindex="auto" type="text" />

<input name="submit" type="submit" value="Submit">
</form>

create_profile.php

<?php
// ob_start(); // uncomment if needed
session_start();
$personal_email = $_POST['personal_email'];
$_SESSION['invalidEmail'] = $personal_email;

if (!filter_var($personal_email, FILTER_VALIDATE_EMAIL)) {
$invalidEmail = 'The email address is invalid or missing' . '<br/>';

echo $personal_email;
echo "<hr>";

}

//Send errors to index.php

echo "";
// header('Location: create_profile.php');

?>

Why "INVALID" does not appear

The error message on the next page is not appearing because of this line:

if ( filter_var($personal_email, FILTER_VALIDATE_EMAIL))
 ---^

which should be:

if (!filter_var($personal_email, FILTER_VALIDATE_EMAIL))

where the ! should have been in front of the f in filter_var to be !filter_var

N.B.: I noticed that session_start(); isn't shown for your index.php file. It must be included in it, if you haven't already done so, and inside all files using sessions.

About the FILTER_VALIDATE_EMAIL filter:

The ! checks for if NOT valid, while the present method is checking if it IS valid.

The ! is a negation unary operator

For a list of logical operators, visit the PHP.net website:

and language operators:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141