MyBB Community Forums

Full Version: Showing a javascript alert
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When you look for a method to show an alert screen from php, the solution that is given everywhere is to trigger a javascript alert by:


echo '<script type="text/javascript">alert("' . $message . '")</script>';


I tested this in a test script and it works perfectly.
When I use it in a MyBB plugin function it works initially too, the popup screen that I get looks like:

[Image: attachment.php?aid=34781]

But when I click OK, the screen goes on blank and execution seems blocked.
When I run the same plugin script but then with the alert line commented, execution goes on without problem.

So it must be something related to javascript executing and the communication to php that javascript has finished. I have looked for applications of this in MyBB, but cannot find these. I think there will be an explanation for this.

I got the idea that it has something to do with page headers, but I can be totally wrong.

Is there I way to let this work?
I'm not sure I understand your problem; dialog boxes like alert stop the execution of JavaScript until a response is received.
(2015-08-03, 10:04 AM)Devilshakerz Wrote: [ -> ]dialog boxes like alert stop the execution of JavaScript until a response is received.

Yes, but the response is clicking OK.

My problem is that this works in a "normal" php script, but not in the MyBB environment. It seems that the php keeps being blocked in spite of the fact there is a response by clicking OK.
JavaScript cannot block PHP since by the time the browser receives the JS code PHP is already executed. It might be that the line with echo '...'; is put in wrong place; this might cause e.g. a PHP error that is suppressed, which prevents the rest of the code from executing. Did you check the error log?
(2015-08-03, 10:38 AM)Devilshakerz Wrote: [ -> ]Did you check the error log?

There I have a problem. My host states that they do not keep a php errorlog and refer to php errors during execution. Sometimes I see php errors in a flash, but then they are overwritten by the next page/screen.

The strange thing is that when I run the same situation except with the alert commented, everything runs smoothly.
Can you show us the code (you can cut out fragments which existence doesn't affect the result)?
I already told that it's about a plugin. When I strip the plugin function to the echo statement only, it shows the alert and goes blank after clicking OK.


$plugins->add_hook("usercp_do_editsig_start", "sigImg_resize_images");
function sigImg_resize_images()
{
	echo '<script type="text/javascript">alert("an alert message")</script>';
}
Currently the <script> is being added at the beginning of the page's HTML and might cause PHP errors;
the usercp_do_editsig_start hook is being executed after the signature update request is received, but when this happens there is no page to be shown and to attach the JavaScript to. If you have Friendly Redirection Pages enabled, this function would attach the JS code to the redirect page - correctly:
function sigImg_resize_images()
{
    global $headerinclude;
    $headerinclude .= '<script type="text/javascript">alert("an alert message")</script>';
} 
Thanks for all efforts, but I tried and no alert screen was shown.
I have friendly redirection pages on.

After the do_editsig, it is redirected to editsig to display the changed signature. I tried to add it to the headerinclude of that page, and that resulted in a message without a blank page after it. Now I'm writing the code that the correct message is shown there.

I will report here when the result is known. Again thanks for your persistence.

It works!!
Communication between plugin/hook functions which operate on different pages is difficult, however. I am not fond of cookies, but in this case I used one.