php - Ajax won't catch Pusher real time event -
i have made pusher messaging demo scheme based of book read. created 3 files, test.html,test.js,post.php. when click submit, ajax not grab , stop refreshing. check pusher log, , php message sent values. lead believe error in test.js. below codes.
test.js
(function($){ var pusher= new pusher('bab47e76c61c0af0818a'), channel = pusher.subscribe('exercise-3-2); channel .bind( 'send-message', function( info ) { var cont= $('#messages'); cont.find('.no-messages').remove(); $('<li>') .html(data.msg) .appendto(cont); } ); $('form').submit(function(){ $.post('post.php', $(this).serialize()); $('#message').val('').focus(); homecoming false; }); })(jquery);
post.php
<?php ini_set('display_errors', 1); error_reporting(e_all); require_once 'pusher.php'; $key = 'bab47e76c61c0af0818a'; $secret = 'privateinfo'; $app_id = 'privateinfo'; $pusher=new pusher($key, $secret, $app_id); if (isset($_post['name']) && isset($_post['message'])) { $data =array( 'name'=> htmlentities(strip_tags($_post['name'])), 'msg'=> htmlentities(strip_tags($_post['message'])), ); $pusher->trigger('exercise-3-2', 'send-message', $data); }
and test.html
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>test!</title> </head> <body> <header> testing pusher! </header> <section> <form method="post" action="post.php"> <label> name: <input type="text" name="name" placeholder="i.e. john" /> </label> <label> message: <input type="text" name="message" id="message" value="hey!" /> </label> <input type="submit" value="send" /> </form> </section> <aside> <h2> messages!</h2> <ul id="messages"> <li class="no-messages"> no messages yet</li> </ul> </aside> <script src="http://js.pusher.com/2.2/pusher.min.js"></script> <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="test.js"></script> <script> </script> </body> </html>
php html ajax real-time pusher
No comments:
Post a Comment