www.webdeveloper.com
Results 1 to 5 of 5

Thread: Problem reading the textbox value

  1. #1
    Join Date
    Mar 2009
    Posts
    19

    Problem reading the textbox value

    Hi all,

    I could automatically load the system date and time in html text boxes using the below java scripting function in html body onload:

    Code:
    <script language="javascript">
               function msg()
                { 
                 var currentTime = new Date()
       	     var month = currentTime.getMonth() + 1
    	     var day = currentTime.getDate()
    	     var year = currentTime.getFullYear()
                 date1.value = month + "/" + day + "/" + year; //date1 is textbox name and id
    
                  var hours = currentTime.getHours()
                  var minutes = currentTime.getMinutes()
                   if ((minutes < 10) && (hours > 11))
                    {
                      minutes = "0" + minutes
                    }
    
                   time1.value = hours + ":" + minutes + " " + "PM"
                    if ((minutes < 10) && (hours < 11))
                    {
                      minutes = "0" + minutes
                    }
                    time1.value = hours + ":" + minutes + " " + "AM"
                 }
             </script>
    I want my html textboxes input to access database from javascripting. I could able to read other input values except date and time which were automatically loaded. Its prompting me [object] & [object] for date and time in alert. Please find the below code I used for inserting the html textbox data in the MS-Access DataBase from javascripting. Clicking insert button its giving me error "no value given for one or more required parameters". Could you help me how to read the Date & Time text Boxes input from javascripting.

    Code:
       <script language="JavaScript">
              function insertinto()
               {
                 var globe = null;
                 globe = new SGWorld();
                 var xpos = document.all["xcorlon"].value;
                 alert(xpos);
                 var ypos = document.all["ycorlat"].value;
                 alert(ypos);
                 var zpos = document.all["zcorht"].value;
                 alert(zpos);
                 var troupinfon = document.all["trpfile"].value;
                 alert(troupinfon);
                 var contree = document.all["country1"].value;
                 alert(contree);
                 var trpdate = document.getElementById('date1').value;
                 alert(date1);
                 var trptime = document.all["time1"].value;
                 alert(time1);
                 var cn = new ActiveXObject("ADODB.Connection");
                 var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = E:\\FramesTemplates\\troupdatabase.mdb;Persist Security Info=False";
                 cn.Open(strConn);
                 var rs = new ActiveXObject("ADODB.Recordset");
                 var SQL = "insert into troupinf (xcoord,ycoord,zcoord,tropinfor,contry,datetoday,timetoday) values(xpos,ypos,zpos,troupinfon,contree,trpdate,trptime)";
                 rs.Open(SQL, cn);
                 alert(rs(0));
                 rs.Close();
                 cn.Close(); 
              }
    Thanks in advance & Regards,
    Sharat.

  2. #2
    Join Date
    Aug 2009
    Posts
    25
    I believe the problem is the SQL statement you're generating:

    Code:
    var SQL = "insert into troupinf (xcoord,ycoord,zcoord,tropinfor,contry,datetoday,timetoday) values(xpos,ypos,zpos,troupinfon,contree,trpdate,trptime)";
    You've hard-coded the variable names, rather than the values. Try something like this:

    Code:
    ...
    
    var sql = "INSERT INTO troupinf (xcoord,ycoord,zcoord,tropinfor,contry,datetoday,timetoday) VALUES (";
    
    sql = sql + "'" + xpos + "',";
    sql = sql + "'" + ypos + "',";
    sql = sql + "'" + zpos + "',";
    sql = sql + "'" + troupinfon + "',";
    sql = sql + "'" + contree + "',";
    sql = sql + "'" + date1 + "',";
    sql = sql + "'" + time1+ "')";
    
    ...
    (hint: I would STRONGLY recommend typecasting the variables before submitting this SQL into any database...parseInt and replace are your best friends)
    Last edited by Alzheimers; 08-12-2009 at 09:54 AM.

  3. #3
    Join Date
    Mar 2009
    Posts
    19
    Thank you for reply Alzheimers,

    Yesterday by googling (http://www.webdeveloper.com/forum/ar...hp/t-1957.html) I found the below code and could insert the record in MSAccess DataBase in JavaScript Client side. I will also check with the code given by you.

    Code:
        var strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = E:\\FramesTemplates\\troupdatabase.mdb;Persist Security Info=False";
                 cn.Open(strConn);
                 //alert(strConn);
                 var rs = new ActiveXObject("ADODB.Recordset");
                 //var SQL = "insert into troupinf (xcoord,ycoord,zcoord,tropinfor,contry,[datetoday],[timetoday]) values(xpos,ypos,zpos,troupinfon,contree,trpdate,trptime)";
                 var SQL = "Select * From troupinf";
                 rs.Open(SQL, cn, 1, 3);
                 rs.AddNew;
                 rs.Fields("xcoord").value = xpos;
                 rs.Fields("ycoord").value = ypos;
                 rs.Fields("zcoord").value = zpos;
                 rs.Fields("tropinfor").value = troupinfon;
                 rs.Fields("contry").value = contree;
                 rs.Fields("datetoday").value = trpdate;
                 rs.Fields("timetoday").value = trptime;
                 rs.Update;
                 //alert(rs(0));
                 alert("Record Inserted");
                 rs.Close();
                 cn.Close();
    Thanks & Regards,
    Sharat

  4. #4
    Join Date
    Mar 2009
    Posts
    19
    Hi Alzheimers,

    Now I need to retrieve data from MSAccess Database and pass each field value to JavaScript Variable. Any idea for doing this?

    Thanks & Regards,
    Sharat.

  5. #5
    Join Date
    Aug 2009
    Posts
    25
    I'm happy to help, but this is straying into ASP/ADO methods that aren't really relevant in a Javascript forum.

    A great resource for help with ADO can be found at W3Schools.com, or in the appropriate forum here.

    To get data from the DB into Javascript, the easiest (but least secure) method is to use VB/C# to run the query and output the results of your SELECT query as a global javascript array.

    Try something along these lines:
    Code:
    <%
    sql = "SELECT * FROM troupinf"
    rs.Open (sql, cn)
    results = rs.GetRows()
    response.write("<script type='text/javascript'>")
    response.write("var a = new Array(" & ubound(results,1) & ");")
    for i = 0 to ubound(results,1)
     response.write("a[" & i & "] = new Array(" & ubound(results,2) & ");")
     for j = 0 to ubound(results,2)
      response.write("a[" & i & "][" & j & "]=" & results(i,j) & ";" & vbCrLf)
     next
    next
    response.write("</script>")
    %>
    Last edited by Alzheimers; 08-13-2009 at 12:23 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles