#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Canada
    Posts
    19
    Rep Power
    0

    Red face php-javascript passing variables?!?!


    I am developing a web application in php/javascript/dom... Recently I discovered the benefits of using dom with javascript... ie:being able to edit data without refreshing the browser, and seeing that my goal is to develop a fast application that works over an intranet it seemed that dom was the answer.

    Now what I would like to do is start passing these javascript variables to php which is activated through javascript functions. Well my problem is basic, I have tried just about everything and I can’t seem to pass variables from javascript to php or from php to javascript?!?!?!

    Also when I have php attached in javascript functions it seems to run once and only once.

    Example:
    <HTML>
    <button onClick="php_script();">PHP_VAR_TEST</button>

    <script>
    var php_string =”0”;

    function php_script()
    {
    php_string =”<?$string1=string1+1;echo string1;?>”;
    alert(php_string);
    }

    </script>
    </HTML>

    Now, when I call the above script through a button command all I ever seem to get is the value 1. Every time I activate this command with the button. WHY? Which does the value of +1 only work once?

    Can anyone out there explain what I am doing wrong and how I can make this possible?

    Thanks in advance….

    Chad
  2. #2
  3. Viking
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Norway
    Posts
    116
    Rep Power
    14
    Keep in mind, PHP is a server side language, javascript is client side. If you need to run php you will have to load the page and run php that way. Alternatively you can run the php scripts in popup windows and return them to your main window.
    All power to the vikings!
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Canada
    Posts
    19
    Rep Power
    0

    Lightbulb a pop up window!!


    That's a great idea!

    Do you think it would be possible to have an invisible php frame on my page...and post data from javascript to that frame...then from that frame back to javascript?!?!

    Does that make any sense?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Australia
    Posts
    52
    Rep Power
    13

    Same question


    Hi, I got samiliar web development on hand, how can I do the popup windows to get back the parameter value to the main php script?

    As I want to develop a color panel, when the user click the image with 216 colors, then the script can return the particular color HEX (RGB) value to main program, that can help to change the font color. What I concern is how to use popup window or alternative way to get the color HEX value back to the main script? Thanks!
  8. #5
  9. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    62

    Re: a pop up window!!


    Originally posted by sparkrulez
    That's a great idea!

    Do you think it would be possible to have an invisible php frame on my page...and post data from javascript to that frame...then from that frame back to javascript?!?!

    Does that make any sense?
    Yes. Keep in mind, though, that nothing happens on the PHP side unless a request is made to the webserver. So, the way to think of it is that your hidden frame will constantly be refreshed, every time you want to use Javascript to send some variables to PHP. No need to actually POST values, but you can to that if you want. More easily, though, you can use Javascript to reload a frame, or more importantly to call the same frame with a different request string. So every time you want to send data to PHP, you will simply do something like

    Code:
    parent.framename.document.location = "hidden.php?varname1=" + var1 + "&varname2=" + var2
    
    // etc... you might have to escape() the value of 
    // your variable before putting it on the query string.
    Then, on the PHP side, your 'hidden.php' file can send variables back to Javascript every time it is called. Thus, even though Javascript is client-side and PHP is server-side, you can have something functionallly equivalent to realtime communication between the two.
    The real n-tier system:

    FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

    Amazon wishlist -- rycamor (at) gmail.com
  10. #6
  11. Viking
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Norway
    Posts
    116
    Rep Power
    14

    Re: a pop up window!!


    Originally posted by sparkrulez
    That's a great idea!

    Do you think it would be possible to have an invisible php frame on my page...and post data from javascript to that frame...then from that frame back to javascript?!?!

    Does that make any sense?
    I can't see any reason why it should'nt work. Just make a 1 pixel high frame at the top and give it a name, then post to that frame and make it post back to the main page. Keep in mind, any time you need to run PHP it must be done serverside.
    All power to the vikings!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    42
    Rep Power
    13
    There are a few work arounds for using JavaScript & PHP together in real-time. The best way that I've found is to use a DOM based JavaScript that every X seconds writes a new JavaScript on the page in real-time and removes the old one.

    There is a basic article here: http://www.dhtmlcentral.com/tutorial...ials.asp?id=11

    Another way to use the two together is to write you're JS as a PHP file and access it like:
    <script type=\"text/JavaScript\" src=\"js_poll_server.php?id=$idSession\"></script>

    This allows you do just about anything in the JS file. Combine that with the technique I mentioned above and you have almost real-time client/server interaction.

    Using these techniques and a few other hacks, I was able to write an instant messaging system like MSN using PHP, JavaScript and MySQL.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Canada
    Posts
    19
    Rep Power
    0

    Thumbs up now that's cool


    I give you two thumbs up for that commment. I was also wondering...I am relatively new to programming in dom...but I am a good coder...I use Homesite to do all my work...

    What I wanted to know was if there is was a program that I could get that would allow me to see the output of the dom...
    so the new javascript... as I was coding?

    .
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    42
    Rep Power
    13
    I've not heard of one that gives that sort of functionality, but I'm way out of touch with the new editors

IMN logo majestic logo threadwatch logo seochat tools logo