javascript/ajax to mysql db

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #46
    The reason it opens a new window is the target attribute. If you remove that, it won't open a new window. However, having said that, onsubmit should be added to the form tag, not the submit button. Remind me, did you want to prevent a page unload completely or did you want to make one submit (with Ajax) and then another page unload/reload one later with Paypal?

    Note: 'urlparams' should be just urlparams without the quotes.

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #47
      i just want to save the form data to a db whilst paypal opens in a new window....nothi ng to do with paypal is going to change, just need to be able to get it to send data to db in the same page or in a seperate page.

      change httpRequest.sen d('urlparams'); to httpRequest.sen d(urlparams);

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #48
        I just noticed that when you call makeRequest(), you're passing an empty string instead of the URL.

        Comment

        • anfetienne
          Contributor
          • Feb 2009
          • 424

          #49
          ok so i have to take away the quotes on urlparams and makeRequest() should be makeRequest(url )?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #50
            Yes and no. Pass the actual URL as a string, e.g.
            Code:
            makeRequest('test.php')

            Comment

            • anfetienne
              Contributor
              • Feb 2009
              • 424

              #51
              ok will do a test and let you know the results

              Comment

              • anfetienne
                Contributor
                • Feb 2009
                • 424

                #52
                ok will do a test and let you know the results

                Comment

                • anfetienne
                  Contributor
                  • Feb 2009
                  • 424

                  #53
                  i can't get this to work on submit.....

                  how many functions can i have on one action?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #54
                    As many as you want; you must be returning from one of the functions. Post your code.

                    Comment

                    • anfetienne
                      Contributor
                      • Feb 2009
                      • 424

                      #55
                      its the same code as what i posted earlier there is no change.....id just like to know how many functions i can run in a onclick and how.

                      do i seperate with commas? eg onclick="func1 , func2"

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #56
                        Originally posted by anfetienne
                        its the same code as what i posted earlier there is no change.....id just like to know how many functions i can run in a onclick and how.

                        do i seperate with commas? eg onclick="func1 , func2"
                        You separate them, like you would normal statements in the language, i.e. with semi-colons.

                        Code:
                        <a href="#" onclick="alert('hi'); func1(); func2(); return false;">...</a>

                        Comment

                        • anfetienne
                          Contributor
                          • Feb 2009
                          • 424

                          #57
                          is it possible to do the same using onclick within a form? sorry, i should of phrased myself properly before

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #58
                            Originally posted by anfetienne
                            is it possible to do the same using onclick within a form? sorry, i should of phrased myself properly before
                            It's possible with any element. Learn by trying, anfetienne. ;)

                            Comment

                            • anfetienne
                              Contributor
                              • Feb 2009
                              • 424

                              #59
                              lol i know the saying.....just needed to know if it was possible

                              thanks for the help

                              Comment

                              • anfetienne
                                Contributor
                                • Feb 2009
                                • 424

                                #60
                                I've tested this and it does not work....i have tried using onsubmit, onclick, onmousedown, onmouseup and it still doesn't do anything.

                                i.e. onsubmit="makeR equest('data.ph p')"

                                here is my coding....am i doing something incorrectly?

                                Code:
                                <script type="text/javascript" language="javascript">
                                    function makeRequest(url) {
                                        var httpRequest;
                                
                                        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                                            httpRequest = new XMLHttpRequest();
                                            if (httpRequest.overrideMimeType) {
                                                httpRequest.overrideMimeType('text/xml');
                                                // See note below about this line
                                            }
                                        } 
                                        else if (window.ActiveXObject) { // IE
                                            try {
                                                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                                            } 
                                            catch (e) {
                                                try {
                                                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                                                } 
                                                catch (e) {}
                                            }
                                        }
                                
                                        if (!httpRequest) {
                                            alert('Giving up :( Cannot create an XMLHTTP instance');
                                            return false;
                                        }
                                		
                                		//enter var details for fields here
                                		var urlparams = "first_name=" + encodeURIComponent(document.getElementById("first_name").value);
                                		urlparams += "&last_name=" + encodeURIComponent(document.getElementById("last_name").value);
                                		urlparams += "&address1=" + encodeURIComponent(document.getElementById("address1").value);
                                		urlparams += "&address2=" + encodeURIComponent(document.getElementById("address2").value);
                                		urlparams += "&city=" + encodeURIComponent(document.getElementById("city").value);
                                		urlparams += "&state=" + encodeURIComponent(document.getElementById("state").value);
                                		urlparams += "&zip=" + encodeURIComponent(document.getElementById("zip").value);
                                		urlparams += "&os0=" + encodeURIComponent(document.getElementById("os0").value);
                                		urlparams += "&email=" + encodeURIComponent(document.getElementById("email").value);
                                
                                		
                                        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
                                        httpRequest.open('POST', url, true);
                                        httpRequest.send('urlparams');
                                
                                    }
                                
                                    function alertContents(httpRequest) {
                                
                                        if (httpRequest.readyState == 4) {
                                            if (httpRequest.status == 200) {
                                                alert(httpRequest.responseText);
                                            } else {
                                                alert('There was a problem with the request.');
                                            }
                                        }
                                
                                    }
                                </script>

                                Comment

                                Working...