javascript/ajax to mysql db

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    javascript/ajax to mysql db

    could anyone tell me the mthod to use javascript/ajax to post to a sql database? or could someone point me to a tutorial?
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    You can have a look at this.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      You'll need some server-side processing to communicate with a db.

      Comment

      • anfetienne
        Contributor
        • Feb 2009
        • 424

        #4
        ive got the code to communicate with a database....tha nks ill take a look

        Comment

        • anfetienne
          Contributor
          • Feb 2009
          • 424

          #5
          i've read over this site a few times....it doesn't have any details of what i need.

          i basically need the details of a form to be duplicated on mouseover of the submit button once all fields have been filled in (i already wrote a code to check if form fields have been completed)....i t takes the values from each field and sends it to a php page so that i can add it to a var and then enter it into a database.

          the method you showed me is the right one but i don't have a clue where to start when it comes to forms as this has nothing on forms

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            What kind of form elements do you have? Text boxes, hidden fields, password and textarea are easy:
            Code:
            "fieldname=" + encodeURIComponent(document.getElementById("fieldID").value);
            To get the values for other elements, e.g. select (single), get the value, and select (multiple), check the selected options. For check boxes and radio buttons, you need to find the checked buttons.

            Comment

            • anfetienne
              Contributor
              • Feb 2009
              • 424

              #7
              im using hidden fields to duplicate the value from visible fields into onChange....im looking for a way the values can go into php without having to refresh or go to a new page. if this cant be done can 1 form submit to two different pages?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                There's no need to duplicate values. You can use the values from the visible fields. Show your Ajax code and I can suggest changes.

                Comment

                • anfetienne
                  Contributor
                  • Feb 2009
                  • 424

                  #9
                  i dont have any....the only ajax examples i could find were to xml.....none to php so i don't even know where to start.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    All you have to do is replace the XML file with a PHP one, e.g. the URL would be test.php instead of test.xml.

                    Comment

                    • anfetienne
                      Contributor
                      • Feb 2009
                      • 424

                      #11
                      so if i took this code here which is an xml example....all i would have to do is change the file name at the end?

                      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;
                              }
                              httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
                              httpRequest.open('GET', url, true);
                              httpRequest.send('');
                      
                          }
                      
                          function alertContents(httpRequest) {
                      
                              if (httpRequest.readyState == 4) {
                                  if (httpRequest.status == 200) {
                                      alert(httpRequest.responseText);
                                  } else {
                                      alert('There was a problem with the request.');
                                  }
                              }
                      
                          }
                      </script>
                      <span
                          style="cursor: pointer; text-decoration: underline"
                          onclick="makeRequest('test.xml')">
                              Make a request
                      </span>

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Originally posted by anfetienne
                        so if i took this code here which is an xml example....all i would have to do is change the file name at the end?
                        that's correct.

                        [end limit]

                        Comment

                        • Ciary
                          Recognized Expert New Member
                          • Apr 2009
                          • 247

                          #13
                          and maybe the mimetype, but i dont think you need it.

                          to make a request object you need this:
                          Code:
                          var httpRequest;
                          
                          try{
                          	httpRequest = new ActiveXObject("MSXML2.XMLHTTP");
                          }catch(exception1){
                          	try{
                          		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                          	}catch(exception2){
                          		httpRequest = false;
                          	}
                          			
                          	if(!httpRequest && window.XMLHttpRequest){
                          		httpRequest = new XMLHttpRequest();
                          	}
                          }
                          then your php will contain an update-query.

                          Code:
                          $id = mssql_connect('***.***.***.***,1433','mssqluser','passwd');
                          mssql_select_db("myDB",$id);
                          
                          result = mssql_query("UPDATE table_name SET column_name = ".$_GET['value']." WHERE id = ".$_GET['id']);
                          
                          mssql_close($id);

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            That's what you would have to do to make a request to a PHP file, but in that PHP file, it should contain the code to deal with the passed values. The change you have to make to pass form values would be in makeRequest before you open:
                            Code:
                            url += "&fieldname=" + encodeURIComponent(document.getElementById("fieldID").value);
                            assuming you already have a parameter, e.g. "file.php?var=1 ".

                            Comment

                            • anfetienne
                              Contributor
                              • Feb 2009
                              • 424

                              #15
                              by the looks of what you're saying ciary i have to replace this with your first piece of coding.

                              Code:
                                       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) {}
                                           }
                                       }
                              is that correct?
                              Last edited by acoder; Apr 30 '09, 01:14 PM. Reason: Fixed code tags

                              Comment

                              Working...