HTML Database access using Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aleseus
    New Member
    • Feb 2010
    • 7

    HTML Database access using Javascript

    Trying to create an HTML page called Project.html that will ask the user for basic information (name, e-mail address, and comments), and when the SUBMIT buttom is hit, add the users to an Access 2007 Database. Ideally, the information would only be able to be added once, but I need the adding sequence to work first. Any and all comments or questions would be appreciated.

    Anthony
    Beginning Programmer

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Project input Form</title>
        <style type="text/css">
            .style1
            {
                width: 70%;
            }
            .style2
            {
                text-align: right;
                width: 261px;
            }
            #Text1
            {
                width: 191px;
            }
            #Text2
            {
                width: 189px;
            }
            #Text3
            {
                width: 190px;
            }
            #Button1
            {}
        </style>
        <SCRIPT LANGUAGE="JavaScript">
        function addrecord (form) {
            var cn = new ActiveXObject("ADODB.Connection");
            var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\aknab\My Documents\testingapp\users.accdb;Persist Security Info=False";
            cn.Open(strConn);
            var rs = new ActiveXObject("ADODB.Recordset");
            var SQL = "select count(*) from Customers";
            rs.Open(SQL, cn);
            alert(rs(0));
            rs.AddNew 'Prepare the database to add a new record and add
            rs.Fields("name") = Request.Form("cname");
            rs.Fields("email") = Request.Form("cemail");
            rs.Fields("comments") = Request.Form("ccomments");
    
            rs.Update;   'Save the update
    
            rs.Close();
            cn.Close();  
        }
        </script>
    </head>
    <body>
    
        <p style="text-align: center">
            Vetsulin Test page.<br />
        </p>
        <table class="style1">
            <tr>
                <td class="style2">
                    Name&nbsp;             </td>
                <td>
                    &nbsp; &nbsp;<input id="Text1" type="text" name="cname" /></td>
            </tr>
            <tr>
                <td class="style2">
                    e-mail&nbsp;
                </td>
                <td>
                    &nbsp; &nbsp;<input id="Text2" type="text" name="cemail" /></td>
            </tr>
            <tr>
                <td class="style2">
                    Comments&nbsp;
                </td>
                <td>
                    &nbsp; &nbsp;<input id="Text3" type="text" name="ccomments"/></td>
            </tr>
        </table>
    
        <p style="text-align: center">
            <input id="Submit1" type="submit" value="submit" onClick="addrecord(this.form)" />&nbsp;&nbsp;&nbsp;
            <input id="Reset1" type="reset" value="reset" /></p>
    
    </body>
    </html>
    Last edited by Dormilich; Feb 2 '10, 05:07 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you are aware, that this will only work in IE?

    Comment

    • aleseus
      New Member
      • Feb 2010
      • 7

      #3
      Yes. this will only be used in a closed environment at this time. the office is Microsoft central.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        What problems are you having with the code? Error messages? Which line number?

        Have you checked the archives for similar questions, e.g. this one?

        Comment

        • aleseus
          New Member
          • Feb 2010
          • 7

          #5
          At launch, I am getting the error at line 33 EXPECTED HEXADECIMAL DIGIT

          when running, and I hit submit, I get an error at line 83 OBJECT EXPECTED

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            What lines do these correspond to in the code?

            I think part of your problem is using XHTML and including JavaScript code within the page. Either use HTML or include the code from a script file, and validate the page.

            Comment

            • ifedi
              New Member
              • Jan 2008
              • 60

              #7
              Code:
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
                
              <html xmlns="http://www.w3.org/1999/xhtml"> 
              <head> 
                  <title>Project input Form</title> 
                  <style type="text/css"> 
                      .style1 
                      { 
                          width: 70%; 
                      } 
                      .style2 
                      { 
                          text-align: right; 
                          width: 261px; 
                      } 
                      #Text1 
                      { 
                          width: 191px; 
                      } 
                      #Text2 
                      { 
                          width: 189px; 
                      } 
                      #Text3 
                      { 
                          width: 190px; 
                      } 
                      #Button1 
                      {} 
                  </style> 
                  <SCRIPT LANGUAGE="JavaScript"> 
                  function addrecord (form) { 
                      var cn = new ActiveXObject("ADODB.Connection"); 
                      var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\aknab\My Documents\testingapp\users.accdb;Persist Security Info=False"; 
                      cn.Open(strConn); 
                      var rs = new ActiveXObject("ADODB.Recordset"); 
                      var SQL = "select count(*) from Customers"; 
                      rs.Open(SQL, cn); 
                      alert(rs(0)); 
              		
              		//below:why no closing single quote?
                      rs.AddNew 'Prepare the database to add a new record and add 
                      rs.Fields("name") = Request.Form("cname"); 
                      rs.Fields("email") = Request.Form("cemail"); 
                      rs.Fields("comments") = Request.Form("ccomments"); 
                
              		//below:why no closing single quote?
                      rs.Update;   'Save the update 
                
                      rs.Close(); 
                      cn.Close();   
                  } 
                  </script> 
              </head> 
              <body> 
                
                  <p style="text-align: center"> 
                      Vetsulin Test page.<br /> 
                  </p> 
                  <table class="style1"> 
                      <tr> 
                          <td class="style2"> 
                              Name&nbsp;             </td> 
                          <td> 
                              &nbsp; &nbsp;<input id="Text1" type="text" name="cname" /></td> 
                      </tr> 
                      <tr> 
                          <td class="style2"> 
                              e-mail&nbsp; 
                          </td> 
                          <td> 
                              &nbsp; &nbsp;<input id="Text2" type="text" name="cemail" /></td> 
                      </tr> 
                      <tr> 
                          <td class="style2"> 
                              Comments&nbsp; 
                          </td> 
                          <td> 
                              &nbsp; &nbsp;<input id="Text3" type="text" name="ccomments"/></td> 
                      </tr> 
                  </table> 
                
                  <p style="text-align: center"> 
                      <input id="Submit1" type="submit" value="submit" onClick="addrecord(this.form)" />&nbsp;&nbsp;&nbsp; 
                      <input id="Reset1" type="reset" value="reset" /></p> 
                
              </body> 
              </html>
              Let me confess that I haven't actually taken time to test your code.
              However, please note the two lines of comment I've placed in your javascript above.

              Above all, I'm not sure I'd expect
              Code:
               <input id="Submit1" type="submit" value="submit" onClick="addrecord(this.form)" />
              to work when this input and all the other input fields are NOT ENCLOSED IN ANY <form></form>
              I suspect that the parent FORM is the object expected

              Regards, Ifedi.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                to work when this input and all the other input fields are NOT ENCLOSED IN ANY <form></form>
                I suspect that the parent FORM is the object expected
                form elements do not submit if there’s no <form> element, that’s right.

                Comment

                • raleti
                  New Member
                  • Jan 2015
                  • 1

                  #9
                  use "microsoft.ace. oledb.12.0" driver instead of "Microsoft.Jet. OLEDB.4.0"
                  and chanage rs.Open(SQL, cn); to rs.Open(SQL, cn,1,3);

                  its worked from with above changes.

                  Comment

                  Working...