Hello Community,
I was wondering if there is a way to call a pNotify notification using php. So after I submit a form I can use PHP to create the popup.

Member Avatar for iamthwee

Use jquery $.ajax

Could you please give me an example.

Calling PNotify is as simple as using the onclick event. Example:

<button onclick="new PNotify({
    title: 'Yayyy you clicked me',
    text: 'Haha, made you click'
});">Click Me!</button>

I'm guessing your main concern is submitting the form. For that, as iamthwee suggests, you'll have to use jQuery's AJAX function. That function allows you to have what's called a "callback" function, which essentially is a function that gets called once everything the ajax function needs to do is completed (which in this case is submitting the form) so your callback function would be a function that calls PNotify.

Here's a good tutorial for ajax submit:
http://code.tutsplus.com/tutorials/submit-a-form-without-page-refresh-using-jquery--net-59

Oh yeah, I was going to use that but I was looking to see if there was a way to do it the way I wanted. Well that sucks, Oh well.

Well you're trying to use a server-side language to fire a client-side event. Before the tweaks can be implemented to make that happen, you have to tell us what exactly your scenario is. Why are you trying to trigger PNotify with PHP?

I want to save settings (to a database) but I require the page to submit (refresh) then call a popup (after the form submit) talling the using their settings have been saved.

Before I give you the solution, I just wanna clariy a few things in reference to the title of this thread. Your intentions with "Calling pNotify In PHP" were correct but when you're trying deal with a client-side language like JS with a server-side language like PHP, it's not a matter of whether PHP can "call" a JS function, but more a matter of PHP just displaying a JS function like normal text, and letting the browser do what it does.

So here you are:

<?php
    if(isset($_POST['foo']) && $_POST['foo'] != "") {
        $query = WRITE_TO_DB_QUERY
        $result = mysqli_query($query);

        if($result) {
            // notice how I'm just writing regular JS to the page
            echo "
                <script type='text/javascript'>
                    $(document).ready(function(e) {
                        notifyUser('success');
                    });
                </script>";
        } else {
            echo "
                <script type='text/javascript'>
                    $(document).ready(function(e) {
                        notifyUser('failure');
                    });
                </script>";
        }
    }
?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="foo" />
    <input type="submit" value="Submit" />
</form>


<script type="text/javascript">
    function notifyUser(message) {
        if(message == "success") {
            new PNotify({
                title: 'Popup Title',
                text: 'Your form as been submitted'
            });
        } else {
            new PNotify({
                title: 'Popup Title',
                text: 'Whops, you messed up'
            }); 
        }
    }
</script>

I know I don't have to say this, but ofcourse, make sure you have jQuery and PNotify scripts included in your page.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.