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
  • 259
  • 1
  • 6
  • 21

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,372
  • 15
  • 66
  • 132