1

I'm using this toast message plugin in a PHP project. http://akquinet.github.io/jquery-toastmessage-plugin/

What I need is trigger the message from the PHP validation / query result. Like:

if ($data === FALSE) { // No product found with the called id. Return to the catalog page throwing error message

    // Trigger Message from here ...

    header('location: products.php');
}

If I simply echo the script call code or include in template like this:

$message = "
    <script>
        $().toastmessage('showToast', {
        text     : 'No product found with the specified criteria',
        sticky   : 1,
        position : 'top-right',
        type     : 'Error',
        closeText: '',
        close    : function () {
            }
        });
    </script>";

echo $message;

it works, but the problem arises when the page gets refreshed (as stated in the example) to ensure no resubmission of the form then the echo gets lost as on refresh it runs out of the validation process and the message doesn't appear.

Any way to handle this?

6
  • Well then pass a parameter in the URL you are redirecting to, so that on next run of your PHP script you can determine whether or not to output that JS code. Commented Mar 26, 2014 at 11:46
  • I don't think it is possible to forward all those tags and notations through $_GET, it will break in between. Do you have any example that you can provide. Commented Mar 26, 2014 at 11:49
  • I did not say you should pass the JS code itself as parameter, but only a value that lets you decide on the next page whether to output the JS code or not. products.php?showMessage=1 or something like that. Commented Mar 26, 2014 at 11:52
  • Situation is as you can see there are multiple parameters like message type (there are 4), sticky or not, positioning, and the entire message (if I want to use the method constantly some of the messages are very lengthy, even with html, like a complete validation error report having 14 error messages in an unordered list etc. which is really not the way I want to make a message display. Do you think it is good to get all the messages with the parameters visible in the url? Commented Mar 26, 2014 at 12:02
  • 1
    Nothing in your original question said anything about parameters or variable parts of the message – so please mention relevant facts straight away next time. // You could simply put all those parameters into the session, then display the dialog with those values on the next page, and remove it from the session afterwards. (The user might have multiple tabs open, sharing the same session – so if that’s a concern, then you could give the specific message an id within your session, and then pass that ID via URL.) Commented Mar 26, 2014 at 12:06

1 Answer 1

2

I'm posting what I've come up with so far (as per discussion in meta) for other's reference if anyone can suggest a better solution or improvement of it:

I made a script init.php that gets loaded with every page and in that page I've included:

        // Alert message display
        if(!isset($_SESSION['msgdisp'])){
            $_SESSION['msgdisp'] = 0;
        }else if($_SESSION['msgdisp'] > 0){
            echo $_SESSION['msg'];
            --$_SESSION['msgdisp'];
        }

// Toast message handler
// -----------------------------------------------------------------------
function message($message='Message',$msg_type=3,$msg_trigger=1,$sticky=0)
{
    $msg_type_array = array('Error','Success','Warning','Message');
    $msg_type = $msg_type_array[$msg_type];
    $_SESSION['msgdisp'] = $msg_trigger;
    $_SESSION['msg'] = "
        <script>
            $().toastmessage('showToast', {
            text     : '".$message."',
            sticky   : ".$sticky.",
            position : 'top-right',
            type     : '".strtolower($msg_type)."',
            closeText: '',
            close    : function () {
                }
            });
        </script>";
}

And through PHP I've called the function this way:

if ($data === FALSE) { // No product found with the called id. Return to the catalog page throwing error message

    // Trigger Message from here ...
    message('No product found with the specified criteria',0,2,1); // 2 is the display trigger, set 1 for no page refresh
    header('location: products.php');
}

I've not considered the other point CBroe highlighted in comments that user may have opened multiple window, reason is page refresh is instant and user can hardly do anything with the other windows by the time the page gets refreshed and message gets displayed.

It is working fine now (many thanks to CBroe), if anyone has any thought on it please do share.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.