WE NEED TO TALK...
 Communication between
    PHP & jQuery
JQUERY

• Written   in Javascript

• jQuery   is just a library

   • Event   handling

   • DOM      Manipulation

   • A...
ASYNCHRONOUS DATA


• Data   on demand

• More   responsive app (for some situations)

• Decouple   presentation from data
XML vs. JSON vs. Plain Text vs. HTML



          Q: Which should you use!?

          A: It depends!
XML
            Pros                       Cons


                            • Bulky
• Easily   readable
                ...
HTML
             Pros                         Cons


                              • Bad   for data
• Simple
            ...
PLAIN TEXT
          Pros                           Cons


• Asreadable as you want      • No   semantics
 to make it
    ...
JSON

                                     {
                                         "users":
                           ...
JSON CONTINUED...
                  Pros                     Cons

• Super    fast

• Native   parsing in browser   • Not ...
JSON PARSING

• Always    use a parser!!!

• Never    user eval() – it’s not safe!

• $.parseJSON();     (jquery)     or  ...
JSON CREATION

• Never    generate JSON by hand

• Always   use a library!

• JSON    libs available for almost any langua...
RETURN VALUES



• Always   return something from your back-end

• Better   to return an object rather than a random strin...
EXAMPLES!

• Form   processing

• Front-end   vs. back-end

• Templating

• Debugging

• http://github.com/gplocke/TriPUG-...
FORM PROCESSING


• Asynchronous    form processing

• Doesn’t   interrupt the user’s work flow

• Remember     to use only...
Robert Frost wasn’t a developer



• When  do you use PHP to build a
 view, when do you use JS
Let your front-end do some of the lifting


• HTML    can be created with PHP or JS

• The   HTML must be added to the DOM...
JQUERY TEMPLATES

• Neat

• Simple

• Powerful

• Fast

• http://api.jquery.com/category/plugins/templates/
Back-End View Generation


• Complex    or extensive markup

• One-time   things

• Forms
DEBUGGING


• FireBug   + FirePHP (firephp.org)

• Only   for Firefox

• FirePHPCore    library or Zend Framework
THANKS!


Email: gplocke@gmail.com

Twitter: @gplocke

http://www.broken-notebook.com
Upcoming SlideShare
Loading in...5
×

We Need To Talk: Communicating with PHP and jQuery

5,708

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
5,708
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide






  • - great for things you don’t really care about (log messages)






  • - code example
  • - “The Road Not Taken.” He could only take one. We get to take both!

  • - code example
  • - code example


  • We Need To Talk: Communicating with PHP and jQuery

    1. 1. WE NEED TO TALK... Communication between PHP & jQuery
    2. 2. JQUERY • Written in Javascript • jQuery is just a library • Event handling • DOM Manipulation • Animation • AJAX
    3. 3. ASYNCHRONOUS DATA • Data on demand • More responsive app (for some situations) • Decouple presentation from data
    4. 4. XML vs. JSON vs. Plain Text vs. HTML Q: Which should you use!? A: It depends!
    5. 5. XML Pros Cons • Bulky • Easily readable • Notalways trivial to • Efficient parse • Strict • Can be slow
    6. 6. HTML Pros Cons • Bad for data • Simple • Only for presentation • Great for layout • Hasto be rendered • Flexible every time
    7. 7. PLAIN TEXT Pros Cons • Asreadable as you want • No semantics to make it • What format is the • Can be anything content? • Can be very fast • No context
    8. 8. JSON { "users": [ { "name":"Joey Joe Joe Junior Shabadoo", "userId":"jjjjs", "job":"A random guy at Moe's", • JavaScript Object Notation "age":42 }, { "name":"Mike Rowe", "userId":"mrowe", "job":"All Of Them", • Collection of name/value pairs "age":48 }, { "name":"Vincent Price", "userId":"vprice", "job":"Creepy Guy", • Human Readable "age":82 }, { "name":"Morgan Freeman", "userId":"mfreeman", "job":"God, President, Personal Driver, Inmate", "age":78 } ] }
    9. 9. JSON CONTINUED... Pros Cons • Super fast • Native parsing in browser • Not quite as readable • No superfluous mark up • Not as useful for • Strict passing HTML • Language independent
    10. 10. JSON PARSING • Always use a parser!!! • Never user eval() – it’s not safe! • $.parseJSON(); (jquery) or JSON.parse(); (native) • Parsers are faster than eval(); • In PHP: json_decode();
    11. 11. JSON CREATION • Never generate JSON by hand • Always use a library! • JSON libs available for almost any language (json.org) • Native browser support: JSON.stringify(); • In PHP: json_encode();
    12. 12. RETURN VALUES • Always return something from your back-end • Better to return an object rather than a random string (an array)
    13. 13. EXAMPLES! • Form processing • Front-end vs. back-end • Templating • Debugging • http://github.com/gplocke/TriPUG-PHP-JS-Example
    14. 14. FORM PROCESSING • Asynchronous form processing • Doesn’t interrupt the user’s work flow • Remember to use only where appropriate
    15. 15. Robert Frost wasn’t a developer • When do you use PHP to build a view, when do you use JS
    16. 16. Let your front-end do some of the lifting • HTML can be created with PHP or JS • The HTML must be added to the DOM • Use JS for reusable / repeatable things • Templating
    17. 17. JQUERY TEMPLATES • Neat • Simple • Powerful • Fast • http://api.jquery.com/category/plugins/templates/
    18. 18. Back-End View Generation • Complex or extensive markup • One-time things • Forms
    19. 19. DEBUGGING • FireBug + FirePHP (firephp.org) • Only for Firefox • FirePHPCore library or Zend Framework
    20. 20. THANKS! Email: gplocke@gmail.com Twitter: @gplocke http://www.broken-notebook.com
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×