简体   繁体   中英

How to show a server side error message in a popup using php

I found some of the similar questions here but they are all in C#.

So kindly tell me the easiest way to show the server side error message using popup.

The popup may be anything like thickbox, modalpopup etc...

Here is the sample code. I didn't get the error message in the popup.

<script type="text/javascript" src="thickbox/jquery-latest.js"></script>
<script type="text/javascript" src="thickbox/thickbox.js"></script>
<link href="thickbox/thickbox.css" rel="stylesheet" type="text/css" />



<span class="prtexterror" style="color:#FF0000;display:none;" id="hiddenModalContent" >{$error_login}</span>


{literal}
<script language="javascript" type="text/javascript">



  $(document).ready(function() {
    tb_show("Please, login", "?tb_inline=true&inlineId=hiddenModalContent&height=180&width=300&modal=true", null);
});



</script>
{/literal}

Any other ways are always welcome. thanks in advance

Generally, PHP's going to spew its errors into the output HTML stream when they occur, unless you've got the .ini setting "display_errors" set to off. If you want to display them in a modal popup (alert box, floating div, etc..), you'll have to add the appropriate logic into the script to capture them:

<?php

$val = some_function_that_causes_an_error();
if (error_get_last()) {
    $lasterror = error_get_last();
}

error_get_last() returns an array, follow the link to the PHP docs to see what the format is.

Later on in your page you'd have to convert the error into whatever format you want it displayed. A basic setup would be:

<script type="text/javascript">

var errmsg = <?php echo json_encode($lasterror) ?>;

alert('Server error of type ' + errmsg.type + ' at line ' + errmsg.line + ' in script ' + errmsg.file + ': ' + errmsg.message);

</script>

Also, remember that fatal errors (syntax errors, out of memory, etc...) cannot be trapped and will abort the script, so unless you're accessing the script via AJAX, you're going to get a blank page or a partial page that ends with the PHP error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM