PHP Does Not Display Error Messages

PHP does not display error messages

To turn on errors at the script level, include at the top of your script:

ini_set('display_errors', 1);
error_reporting(~0);

Alternatively, if it is not a production site and simply a development / testing site, you can turn on error reporting in php.ini. Search it for these settings:

error_reporting  =  E_ALL
;error_reporting = E_ERROR
display_errors = On
;display_errors = Off

no error message in php

Try to put this inmediatly after the php tag (line 2), (also in your connect.php file):

error_reporting(E_ALL);
ini_set('display_errors','On');

How do I get PHP errors to display?

This always works for me:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:

display_errors = on

(if you don't have access to php.ini, then putting this line in .htaccess might work too):

php_flag display_errors 1

Error message not displaying html forms using php

The reason why your error is not showing is because you're already inside PHP in doing an echo and have PHP tags.

echo '<span class="error"><?php echo $error;?></span>';

You can either do: (escaping double quotes for the CSS class name)

echo "<span class=\"error\">$error</span>";

or concatenate the $error variable:

echo '<span class="error">'.$error.'</span>';

Plus, variables do not get parsed inside single quotes, unless concatenated.

PHP not displaying errors even though display_errors = On

You also need to make sure you have your php.ini file include the following set or errors will go only to the log that is set by default or specified in the virtual host's configuration.

display_errors = On

The php.ini file is where base settings for all PHP on your server, however these can easily be overridden and altered any place in the PHP code and effect everything following that change. A good check is to add the display_errors directive to your php.ini file. If you don't see an error, but one is being logged, insert this at the top of the file causing the error:

ini_set('display_errors', 1);
error_reporting(E_ALL);

If this works then something earlier in your code is disabling error display.

Why in my WordPress site PHP is not showing any error message

Not sure what your problem is.

If you've just changed setting in php.ini file, make sure that you restarted Apache.

To test your setting, you can trigger an error. E.g: add trigger_error("Something goes wrong here"); in your index.php file.

Or, in case you want to use WP DEBUG, you can take a look here

PHP - Session error messages not showing

Ath the top of your palaute3.php file you have this

$_SESSION["nimiVirhe"]="";
$_SESSION["spostiVirhe"]="";
$_SESSION["palauteVirhe"]="";

So you re-init your session error messages before using this them.

Those 3 lines should be in the validation page, but not in your error page !

Remove that and it will work.

Display error messages and fetched data in php from another php file

As i mentioned earlier in my comment, you require Json to achieve your goal.

I've have made some additional tweaks to your PHP code which are also commented.


Json response structure

{
"success": false,
"errors": [{
"id": "msg",
"msg": "Please correct the given mistakes"
}, {
"id": "firstname_error",
"msg": "Invalid email entered"
}, {
"id": "username_error",
"msg": "Username must be atleast 5 characters long & alphanumeric"
}]
}

or

{
"success": true,
"errors": []
}


To achieve what we want, we must run all the checks individually then assign and return their return result via Json (which can be utilized in JS/jQuery).

We use an array $data that holds the validation status as well as the error messages and the css identifier of the element where the should be displayed.



PHP code:

<?php
include "../config/Database.php";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Checks whether the request type is `POST` rather than check if a field named `submit` is available

// $data will be encoded as json and then returned by the script
$data = array(
'success' => false, // Flag whether everything was successful
'errors' => array() // Provide information regarding the error(s)
);


$firstname = htmlspecialchars(trim($_POST['firstname']));
$lastname = htmlspecialchars(trim($_POST['lastname']));
$email = htmlspecialchars(trim($_POST['email']));
$username = htmlspecialchars(trim($_POST['username']));
$pass = htmlspecialchars(trim($_POST['pass']));

/* We will use individual `if` for all checks below, because
`elseif` stops as soon as one of the conditions returns `true`
and skips the rest of the validation checks, but we need to tell
the user about all errors in one attempt and not one-by-one */

if (empty($firstname) || empty($lastname) || empty($email) || empty($username) || empty($pass)) {
$data['errors'][] = ['id'=>'msg', 'msg' => 'All fields are required'];
// `id` should carry the css id of error div attached to field where the error originated from
} else {
$data['errors'][] = ['id'=>'msg', 'msg' => 'Please correct the given mistakes'];
if (!preg_match('/^[a-zA-Z]*$/', $firstname) || !preg_match('/^[a-zA-Z]*$/', $lastname)){
$data['errors'][] = ['id'=>'firstname_error', 'msg' => 'Numbers and Symbols not allowed in name'];
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$data['errors'][] = ['id'=>'firstname_error', 'msg' => 'Invalid email entered'];
}
if (strlen($pass) < 8) {
$data['errors'][] = ['id'=>'firstname_error', 'msg' => 'Password must use 8 characters'];
}
if (!preg_match("/^[a-zA-Z0-9]+$/", $pass)) {
// I unified your password validation regex pattern
$data['errors'][] = ['id'=>'password_error', 'msg' => 'Password must be alphanumeric'];
}
if (!preg_match('/^[a-zA-Z0-9]{5,}$/', $username)) {
$data['errors'][] = ['id'=>'username_error', 'msg' => 'Username must be atleast 5 characters long & alphanumeric'];
}
}

// Execute only if there are no erros
if (empty($data['errors'])) {
// Validation complete without errors, do the required stuff here
$sql = 'INSERT INTO users (firstname,lastname,email,uname,pass) VALUES (:firstname,:lastname,:email,:uname,:pass)';
$stmt = $conn->prepare($sql);
$result = $stmt->execute([':firstname' => $firstname, ':lastname' => $lastname, ':email' => $email, ':uname' => $username, ':pass' => $pass]);

// Everything done, mark the registration as successful
$data['success'] = true;
}


// Return the data with a `json` mime type instead of html
header("Content-Type: application/json; charset=UTF-8");
echo json_encode((object)$data); // converts array into json

}
?>




jQuery Ajax code:

$(".signupform").submit(function(event) {
event.preventDefault(); // prevent form submitting with page refresh here
$.ajax({
url: 'inc/register.php', // php script's location
type : "POST",
dataType : 'json', // data type
data: $(this).serialize(), // creates a URL encoded text string by serializing form values
success: function(data) {
//Check to see if the validation was successful
if (data.success) {
//Turn the response msg into a success alert
$('#msg').addClass('msg-info').text('Registration Complete');
$('#msg').show(); // removes display:none property
} else {
//There were some errors
//Turn the response alert into an error alert
$('#msg').addClass('msg-error');
// add `msg` to individual fields with there `id` in json
$.each(data.errors, function(){
$('#' + this.id).text(this.msg);
});
$('#msg').show();
}
}
});
});

Your html also needs slight tweaking:

  1. Added a dummy class to form for targetting purposes
  2. Removed unnecessary repetition of error message div

<form class="signupform" action="inc/register.php" method="POST">         <h4 class="black-text">Registration</h4>     <div id="msg" class="msg z-depth-3 scale-transition" style="display:none"></div>     <div class="row">      <div class="input-field col s12">       <input placeholder="Firstname" id="firstname" type="text" name="firstname" class="validate">       <div id="firstname_error" class="val_error"></div>      </div>     </div>     <div class="row">      <div class="input-field col s12">       <input placeholder="Lastname" id="lastname" type="text" name="lastname" class="validate">       <div id="lastname_error" class="val_error"></div>      </div>     </div>     <div class="row">      <div class="input-field col s12">       <input placeholder="Email" id="email" type="text" name="email" class="validate">       <div id="email_error" class="val_error"></div>      </div>     </div>     <div class="row">      <div class="input-field col s12">       <input placeholder="Username" id="username" type="text" name="username" class="validate">       <div id="username_error" class="val_error"></div>      </div>     </div>     <div class="row">      <div class="input-field col s12">       <input placeholder="Password" id="password" type="password" name="pass" class="validate">       <div id="password_error" class="val_error"></div>      </div>     </div>     <button class="btn waves-effect waves-light" type="submit" id="submit" name="submit">Register     </button>     <button class="btn modal-close waves-effect waves-light">Cancel     </button></form>


Related Topics



Leave a reply



Submit