PHP error messages not displaying in browser as in mioddle of JavaScript code ← (PHP, JavaScript, HTML)

I have a web app that builds a web page using PHP.

The program outputs the HTML page header and then the bulk of the code is building and writing JavaScript data and if there is an issue in my code, the HTML-formatted PHP error message is mixed in the with JS code and so does not appear in the browser, so the user gets a blank screen with no error message, and I need to so a 'Show source' to see the message.

Here is an example of the source output by the program with a PHP error message:

' node [fontname = "Helvetica-Bold",             ',
'       color="#ffc68c",                 ',
'       fontcolor="#000000",             ',
'       fontsize=10,                               ',
'       shape=box,                                 ',
'       style="rounded,filled"];       ',
' edge [fontname = "Helvetica-Bold", penwidth="3", color="white"];',
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/u/jocs065/ihsconfig/ihs/jocs065/cgi-bin/phpNoSecure/graphBuild.php</b> on line <b>739</b><br />
'}',
];
</script>

and the PHP error message re the forEach() in the middle, which doesn't come out on the web page being built.

Any way to let the user see this error?

Answer



Solution:

You can either use a Try {} Catch {} block as suggested, or you can also define a custom error handler with set_error_handler. Note that you can also experiment with error_reporting( -1 ) to not display errors or even ini_set( "display_errors", false )

Answer



Solution:

IF I read this correctly, it seems that you are posting (assumption) to a php page to do some stuff, then you are trying to alert the user to the outcome of the php code by means of a message on your original main page?

Use some php variables to create an array status and message that will be returned to your mymainpage.php, try the following -

your main page posted to say dostuff.php //In your dostuff.php page...

 if($_SERVER["REQUEST_METHOD"] == "POST") {
  //Your messages is obviously returned by some sort of checks, this is for demo only - do stuff here...
  $myerror_message = "Warning:  Invalid argument supplied for foreach() in /u/jocs065/ihsconfig/ihs/jocs065/cgi-bin/phpNoSecure/graphBuild.php on line 739";
  $display_message_status = array('status'=>1,'message'=>$myerror_message);

  include_once( '../mymainpage.php' );

  exit;
 } else {
  $display_message_status =  array('status'=>0,'message'=>'Nothing was posted, cannot return any myerror_messages.');
  //as above, 1 is for success, 0 is for false...

  include_once( '../mymainpage.php' );

  exit;
 }

 //In your mymainpage.php...
 if ( isset( $display_message_status )) : 
  ($display_message_status['status'] == true ? $class = 'success': $class = 'error');
 ?>
 <div class="message <?PHP echo $class;  ?>">
  <span><?PHP echo $display_message_status['message'];  ?></span>
 </div>
<?php 
 endif; 
?>

Your dostuff page will return a success or error message inside the div/span for your user to view, if there are no status, the div is not visible.

Source