1

I have a PHP script that has this code snippet:

function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
header('Location: http://provectium.com/error.html');
exit();
}

And I have a HTML Code (error.html) with a <div> in it to display $myError. How can I do this?

I was using <?php echo $myError; ?> inside the PHP script, but I want an separate page since it does not function correctly.

EDIT

function show_error($myError)
{
?>
<!DOCTYPE html>
<html lang="en">
...
<?php echo $myError; ?>
...
</html>

<?php
exit();
}

Is this method also OK to use?

1
  • 1
    make error.html a .php page and parse the error to it. Commented Mar 18, 2014 at 23:37

3 Answers 3

1

Using a html file you could pass the value on query_string:

header('Location: http://provectium.com/error.html?error_message=' . $myError);

And use javascript for showing the error wherever you want If you use a php error file you can do something like redirection above and use php for showing

error.php

echo '<html><body><div>'.$_GET["error_message"].'</div></body></html>';

Trying to be simple..

Sign up to request clarification or add additional context in comments.

Comments

0

For situations such as yours, is often used mechanizm called flash messages. Their meaning is easy to pass a value to another page, and after the first reading delete it. Flash messages a mechanism is implemented using session or cookie.

<?php

/**
 * This function set flash message
 * return true if messege set is succesed and false failed
 * 
 * @var string $message
 * @var int $expire -  default 1 hour 
 * return bool 
 */ 
function set_flash_message($message, $expire= 3600) 
{

  return  setcookie("message", $message, time()+ $expire); 

}

function get_flash_message()
{
  $result = isset($_COOKIE['message']) ? $_COOKIE['message'] : null);
  if($result){
    unset($_COOKIE['message']); // clear
  }
  return $result;
}

now you can set your message called function set_flash_message($problem) and get message called get_flash_message()

UPDATE

function check_input($data, $problem='')
{
 $data = trim($data);
 $data = stripslashes($data);
 $data = htmlspecialchars($data);

 if ($problem && strlen($data) == 0)
 {
    set_flash_message($problem);
    show_error($problem);
 }
  return $data;
 }

function show_error($myError)
{
  header('Location: http://provectium.com/error.html');
  exit();
}

in file error.php

<!DOCTYPE html>
<html lang="en">
 ...
<?php echo get_flash_message(); // make sure this file must contain implement this function ?>
...
</html>

2 Comments

Should I rename the HTML file as a PHP before setting get_flash_message() or you can integrate it?
yes need rename you file to error.php and use <?php echo get_flash_message(); ?> and before this code header('Location: provectium.com/error.html'); set flash message set_flash_message($myError)
0

If you are looking to follow best practices, I highly recommend sticking to the Model-View-Controller approach. Your Controller would then pass the data to the view to handle rendering your error message.

There are plenty of Template engines you can implement for these features. Twig and Smarty provide simple ways of doing this. Then you would create your simple error.html as a template (depending on your templating language).

For example with twig, you write your template file:

//error.html.twig ...
<body>
    <div>
        {{ myError }}
    </div>
</body>
//...

And in your controller you call for it to be rendered with an error:

//index.php ...
if ($problem && strlen($data) == 0)
{
    render('error.html.twig', array("myError" => $myError));
}
//...

Using templating engines provides a powerful way to control how your pages look and how they change with dynamic data.

There are also plenty of informative tutorials on how template engines function if you are looking to understand them better, or would like to create a simple one to achieve your goal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.