简体   繁体   中英

PHP / JS - Echo Js function Notification after PHP curl post

I'm trying to show a Javascript function, which contains a alert/notification after my PHP Curl post went through.

Any idea how to get this working?

My Javascript:

<script>
    $('body').pgNotification({
        style:'bar',
        message: 'added',
        position:'top',
        type:'success',
        timeout:'6000'
    }).show();
</script>

My PHP:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'www.site.com');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'parameters to pass');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile';
    $response = curl_exec($ch);
    curl_close($ch);
?>

Now I want to show the Javascript Notification after the PHP curl post is done.

Sadly I can't simply use a echo 'HTMLCODE'; to show a notification. I need the timeout function in it.

I appreciate every kind of help.

Expecting that you are not calling CURL through AJAX . Just echo your javascript function if CURL response is what you want it to be.

Javascript

function showNotification(){
  $('body').pgNotification({
      style:'bar',
      message: 'added',
      position:'top',
      type:'success',
      timeout:'6000'
  }).show();
}

PHP

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'www.site.com');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'parameters to pass');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile';
    $response = curl_exec($ch);
    curl_close($ch);

    if($response == 'whatever it should return'){
        echo "
        <script>
            // Run this if page is loaded
            // Probably using jQuery?
            $(document).ready(function(){
                showNotification();
            });
        </script>";
    }
?>

 function test() { $('body').pgNotification({ style:'bar', message: 'added', position:'top', type:'success', timeout:'6000' }).show(); } 
 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'www.site.com'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, 'parameters to pass'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile'; $response = curl_exec($ch); curl_close($ch); echo '<script>test();</script>'; ?> 

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