How do you call a PHP script from Javascript ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qw12er
    Confirmed User
    • Apr 2004
    • 799

    #1

    How do you call a PHP script from Javascript ?

    I need to call a PHP Script from a Javascript segment of code. I also need the Javascript to stop it's execution while the PHP script does it job. Once the PHP script is done the Javascript must continue where it was.

    Is such a thing possible. If it's ... how.
    (I know it's possible with sockets, but seems too much trouble that way...)

    Thanks
    I have nothing to advertise ... yet.
  • AdPatron
    No commissions, no fees.
    • Apr 2003
    • 17706

    #2
    PHP is server-side and Javascript is client-side, which means the two will not be able to function together.

    Comment

    • StuartD
      Sofa King Band
      • Jul 2002
      • 29903

      #3
      Originally posted by CollegeSucks
      PHP is server-side and Javascript is client-side, which means the two will not be able to function together.
      that's exactly right.
      This is me on facebook
      This is me on twitter

      Comment

      • qw12er
        Confirmed User
        • Apr 2004
        • 799

        #4
        No it's possible ... in fact it's even easy to do it ...

        in Javascript :
        window.navigate("http://yourPHPScript.php");
        and that will do ...

        but in my case I'm doing it in the onBeforeUnload event. Since I call a other page while the browser is closing the PHP script never receive the info and create an empty record in my DB.

        So i would need something that loops so the onbeforeUnload don't finish until the php script is done. Basically I need a fucntion that block Javascript execution until the new page isn't completly loaded
        I have nothing to advertise ... yet.

        Comment

        • StuartD
          Sofa King Band
          • Jul 2002
          • 29903

          #5
          Well, if you're going to a separate page entirely... what you could do is split your function into two new functions, so the first half gets done... then it calls the php script, and then the php script runs a "onLoad" so that it refers back to the original document and the second half of your javascript.
          This is me on facebook
          This is me on twitter

          Comment

          • Arowanna
            Confirmed User
            • Dec 2004
            • 248

            #6
            try using an iframe

            Comment

            • naitirps
              Confirmed User
              • May 2004
              • 761

              #7
              lots of ways you can get shit like that to work... usually better to stuff in php first then pass to javascript works fine for me all the time... what exactly are you trying to do?
              Programmer
              ICQ 44035273 | AIM spritwork | Email spritian at spritian dot com

              Comment

              • grumpy
                Too lazy to set a custom title
                • Jan 2002
                • 9870

                #8
                you can call an update script from a page in java anytime you want with a little trick

                put an image on a page like this < img src=whatever height=1 width =1 name="trick" >

                in a java script you can do the following
                trick.src = whatever.php

                hope this helps.
                Don't let greediness blur your vision | You gotta let some shit slide
                icq - 441-456-888

                Comment

                • qw12er
                  Confirmed User
                  • Apr 2004
                  • 799

                  #9
                  Originally posted by naitirps
                  lots of ways you can get shit like that to work... usually better to stuff in php first then pass to javascript works fine for me all the time... what exactly are you trying to do?
                  basically I'm trying to monitor everything my surfers do on my web site. (Where they clic, which cat. they prefer etc ... ) Since I don't want to generate a lot of traffic to my database I want to use the surfers computer as much as possible. So the idea is to do as much gathering as possible in Javascript and when the surfer close it's browser (or quit my page) send the information to a php script on my server which will insert the info into my database.
                  I have nothing to advertise ... yet.

                  Comment

                  • StuartD
                    Sofa King Band
                    • Jul 2002
                    • 29903

                    #10
                    Originally posted by qw12er
                    basically I'm trying to monitor everything my surfers do on my web site. (Where they clic, which cat. they prefer etc ... ) Since I don't want to generate a lot of traffic to my database I want to use the surfers computer as much as possible. So the idea is to do as much gathering as possible in Javascript and when the surfer close it's browser (or quit my page) send the information to a php script on my server which will insert the info into my database.
                    I'm not sure I get it quite right, but it seems to me that your server and database will do the same amount of work either which way, just at a different time in the page load. And if this is the case, then perhaps caching stats? Writing to a file? sessions and then session dumping after?
                    I dunno... just seems like having javascript do stuff when they close their browser will result in a LOT of lost data and won't save much if anything on serve load.
                    This is me on facebook
                    This is me on twitter

                    Comment

                    • naitirps
                      Confirmed User
                      • May 2004
                      • 761

                      #11
                      easy way to do it is at the top of every script include a small php inc file that just monitors the ref, dt, ip etc inserting to your db... you can also append what they clicked on in the uri so you can track it that way as well... no need for javascript.

                      obviously if there is no more activity, they've closed the browser, because up until that point you've monitored where they are going in the site
                      Programmer
                      ICQ 44035273 | AIM spritwork | Email spritian at spritian dot com

                      Comment

                      • qw12er
                        Confirmed User
                        • Apr 2004
                        • 799

                        #12
                        Originally posted by NichePay - StuartD
                        I'm not sure I get it quite right, but it seems to me that your server and database will do the same amount of work either which way, just at a different time in the page load. And if this is the case, then perhaps caching stats? Writing to a file? sessions and then session dumping after?
                        I dunno... just seems like having javascript do stuff when they close their browser will result in a LOT of lost data and won't save much if anything on serve load.
                        You're probably right for the amount of data loses but I want to test it ... Cause no it won't do the same amount of work for the server ... If I can use Javascript the way I want I will be able to connect to my database and update it only once instead of "X" numbers of time (depending of what my surfers do on my site).

                        Might represent a good saving (Processing power) if my site gets lot of traffic.
                        I have nothing to advertise ... yet.

                        Comment

                        • qw12er
                          Confirmed User
                          • Apr 2004
                          • 799

                          #13
                          [QUOTE=naitirps]easy way to do it is at the top of every script include a small php inc file that just monitors the ref, dt, ip etc inserting to your db... you can also append what they clicked on in the uri so you can track it that way as well... no need for javascript.
                          QUOTE]

                          I know I could do this like you wrote, but it will result in way to much connection to my database...
                          I have nothing to advertise ... yet.

                          Comment

                          • naitirps
                            Confirmed User
                            • May 2004
                            • 761

                            #14
                            so dump to flatfile, and update hourly to your db then

                            or do it solely in xml...
                            Programmer
                            ICQ 44035273 | AIM spritwork | Email spritian at spritian dot com

                            Comment

                            • AdPatron
                              No commissions, no fees.
                              • Apr 2003
                              • 17706

                              #15
                              Originally posted by qw12er
                              So i would need something that loops so the onbeforeUnload don't finish until the php script is done. Basically I need a fucntion that block Javascript execution until the new page isn't completly loaded

                              Good luck with that -lol.

                              Comment

                              • grumpy
                                Too lazy to set a custom title
                                • Jan 2002
                                • 9870

                                #16
                                cookies is he answer
                                Don't let greediness blur your vision | You gotta let some shit slide
                                icq - 441-456-888

                                Comment

                                Working...