SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP in javascript

    Ugh I've been working too much today. Just can't think anymore... and now this is bothering me. Say I had the following JS:

    Code:
    for (i = 0; i < elements.length; i++) {
        alert(elements[i].value);
    }
    how could I deal with the elements[i].value in PHP? I guess, how can I "inline-pass" that variable to some code?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use Ajax to pass the values to the backend (PHP).

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off the first glance, you cannot do so until you make another HTTP request. JavaScript code is executed on the client side (after PHP has parsed the page and sent off the content) and to pass on certain variables from client to sever would require another request. Possibly, you can create a hidden form and make it to submit the required value to the server in order to be captured by PHP. Additionally, Ajax could come handy here.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by triexa View Post
    Ugh I've been working too much today. Just can't think anymore... and now this is bothering me. Say I had the following JS:

    Code:
    for (i = 0; i < elements.length; i++) {
        alert(elements[i].value);
    }
    how could I deal with the elements[i].value in PHP? I guess, how can I "inline-pass" that variable to some code?
    Give some hints as to what you're working on and maybe I can suggest a good way to do it

  5. #5
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Give some hints as to what you're working on and maybe I can suggest a good way to do it
    http://www.sitepoint.com/forums/showthread.php?t=477961

    Which, thanks to you.. I've got. I just need the part to actually give a value to each overlay

    I could potentially loop through the click trackings and put each tracking into a hidden field, but that might end up proving more cubersome...

    I immediately thought of an AJAX call to get each value, but I was hoping there would be a better way..
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you should write JS to the page with PHP. Keep the main JS that does the overlays in it's own external file, but write the specific data to the page using PHP.
    You should then be able to access that JS object, or array to populate the overlay divs.

    Much better than making an Ajax request for each overlay!

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Sounds like you should write JS to the page with PHP. Keep the main JS that does the overlays in it's own external file, but write the specific data to the page using PHP.
    You should then be able to access that JS object, or array to populate the overlay divs.

    Much netter than making an Ajax request for each overlay!

  8. #8
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but it will bog down the initial js load.

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SimplyFu View Post
    but it will bog down the initial js load.
    A couple lines defining some variables or an array won't bog anything down...

  10. #10
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    A couple lines defining some variables or an array won't bog anything down...
    How do you suggest I do it though? I mean, I assume an array index CAN'T be a URL - with all those weird characters and all
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you'd just look at the source of my overlays you'd see they can

    There's no reason you can't do it with numerically indexed arrays too, just another loop or two to find an entry in that instead of a direct access.

  12. #12
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    If you'd just look at the source of my overlays you'd see they can

    There's no reason you can't do it with numerically indexed arrays too, just another loop or two to find an entry in that instead of a direct access.
    I've technically got it all working, my problem is when I have two links pointing to the same place..

    What I have is applied a div overlay in each one with an image that when hovered shows the other div that contains the data for that link (quite a bit like yours).

    This inner div has an id of stat_(the link url)

    obviously, if I hover over the first one - all is well. The second - it shows the first one... and of course Ids should be unique. Any tips to get through this problem I created?

    P.S. You're extremely helpful

    Kaitlyn
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Don't use the URL as the div identifier. Just increment a number each time you create a new div and use that as the ID. stat_1, stat_2, stat_3...
    Quote Originally Posted by triexa
    P.S. You're extremely helpful
    Hopefully what you're creating isn't direct competition Even if it is, JavaScript is open source by necessity, I wouldn't be able to erect any barriers to entry

  14. #14
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Don't use the URL as the div identifier. Just increment a number each time you create a new div and use that as the ID. stat_1, stat_2, stat_3...


    Hopefully what you're creating isn't direct competition Even if it is, JavaScript is open source by necessity, I wouldn't be able to erect any barriers to entry
    You've helped me greatly. Everything works as it should - now I just need to "pretty things up" and change a few things. Is is obviously tracking for links, but no its not a counter and is just a part of my own software

    As you say - JavaScript is free to all... heh
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter


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
  •