SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Unobtrusive PHP/JAVASCRIPT

    Hello Folks I'm new here and also, if it wasn't enough, I'm a newbee too. I would like to know if there is a technique, such as unobtrusive PHP/Javascript and what is the best way to make these two languages interact without disrupt each other. Actually I want any guidance to where I can look at and be able to write clear and neat code.

    Sorry if I'm saying "non-sense" things . It is cause I have seen so much scarry and ugly scripts when it comes to interact these two languages.

    Txs any help!
    Last edited by treixeraf; Oct 1, 2009 at 06:29. Reason: State better

  2. #2
    SitePoint Addict Ramiro S's Avatar
    Join Date
    May 2003
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all keep in mind that PHP runs in the server side and Javascripts runs in the client side (ex. after the page is rendered by the server and loaded by the client) so basically they will not interact unless you use AJAX

    Keep Javascript files outside the design, include them as .js files in the header of the HTML.

    Things that will make it easier for you to code and maintain both scripts is: always use functions or classes, even for the smallest thing your code does since if you start using AJAX / JQUERY, whatever Javascript to load PHP files you will need to have a solid structure in order to avoid problems and it will also help to debug.
    Quasar - Web Development - Free Avatars

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If your question is about using PHP to output JS, then my advice No.1 would be use the Heredoc syntax to create/assemble your JS in PHP

    PHP Code:

    $a_var 
    "something" ;

    $js = <<<EOL

    <script>alert('
    {$a_var}');</script>

    EOL;

    echo 
    $js 
    That way you at least have a chance of retaining the meaning and order of apostrophes and quotes, important in both PHP and JS.

    The 2nd rule you could adopt is to always start off creating a JS + HTML version of your page which works without errors - even thought it contains concrete test values - THEN get PHP to create output which recreates that static page.

    Do not try and develop PHP and JS in parallel when starting out.

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some frameworks offer classes that integrate different languages with the framework views in a simple matter. For example Zend_Dojo or ZendX_JQuery.

  5. #5
    Follow: @AlexDawsonUK silver trophybronze trophy AlexDawson's Avatar
    Join Date
    Feb 2009
    Location
    England, UK
    Posts
    8,111
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by treixeraf View Post
    Hello Folks I'm new here and also, if it wasn't enough, I'm a newbee too. I would like to know if there is a technique, such as unobtrusive PHP/Javascript and what is the best way to make these two languages interact without disrupt each other. Actually I want any guidance to where I can look at and be able to write clear and neat code.
    treixeraf, Generally neither JavaScript or PHP "interact", JS is usually associated with the client side where as PHP is with the server-side, this means that they don't conflict because JavaScript is only handled after the PHP has been done. The basic premise behind unobtrusive scripting is normally associated with JavaScript because unlike PHP, JavaScript relies on the technology being enabled in order to function (and not everyone does have it turned on). What this means is that you don't want to rely on it being available for any functionality, essentially... keep your JavaScript in external JS files (so not embedded within the page) and only use JS to enhance your content, don't make it so that you need JavaScript for the website to work

  6. #6
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all folks! These apparently little advise will help me out a lot, really. Despite the fact I'm a newbee I know basic concepts of both languages, but I don't know the best way to implement them. Any other advise would be appreciated! THX


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •