TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-04-2010, 03:11 PM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default jQuery and instant submit

You would think this would be one of the most common things to find a tutorial on, but I'm googling up a storm with absolutely no payoff.

I still haven't gotten around to learning how to do this, and I'm sure the concept would be easy if I did, but on the upside, the books ARE on order.

In the meantime, I would like to find a tutorial that shows me how to use jQuery similar to what this forum does with responses and the same as many comment systems, or even twitter for that matter. I want to submit a small 200 character text area and have it show up above the form instantly.

No, I'm not trying to create a social media site. Just a simple online task list for myself and I'd like to add the functionality to just spam it with entries and have them show up as I submit them. The PHP is easy, but I can't find a tutorial on how to do the ajax side.
delayedinsanity is offline  
Reply With Quote
Old 04-04-2010, 11:03 PM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Wrote this up right quick...might be rife with syntactical errors

This is really just a matter of DOM traversal which is devilishly easy with the Sizzles library ( It's what jQuery uses under the hood to traverse DOM also developed by Resig )

We simply do a post to the script and wait for the response, one we get it back, we format it to our liking and insert this new element AFTER the last post item.

jQuery provides an easy method of doing this

element:last


js Code:
jQuery(document).ready(function(){
   jQuery('button[name="postTask"').click(function(e){
        e.preventDefault();
       
        var form = jQuery('form[name="task_form"]');
        jQuery.post('script.php', form.serialize(), function(response){
            if( response !== null ){
                /* We've gotten back date, we COULD use the data provided in the form variable
                 * but the script might be doing some special data manipulation so, we use the response data
                 * waiting for post back also gives the script the chance to disallow this action
                 * and simply return null
                 */
                 
                 // Assuming it returned in JSON
                 jQuery('<div>').addClass('task-item')
                                .append(
                                    jQuery('<h2>').addClass('task-item-header')
                                                  .html(response.title)
                                )
                                .append(
                                    jQuery('<div>').addClass('task-item-content')
                                                   .html(response.body)
                                )
                                .insertAfter('.task-item:last');                   
            }else{
                alert('Psssttt...you can\'t post items bucko');
                console.log('...because you suck');
            }
        });
       
   });
});
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 04-05-2010, 01:59 AM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Now this may sound silly, and I appreciate the help, but how do you pass data from the php back to the jquery that called it? json_encode and set it to a Javascript variable called response?
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design