Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How do I get a javascript to post data back to same page

    I have this JavaScript that is pulling data based on input from the users. It is working very well and returns the data based on the criteria input by users, but it is opening a new tab and shownig the data instead of returning to the same page and displaying the data as needed.

    Here is my Javascript that I am using which works.

    Code:
    var connection = new ActiveXObject("ADODB.Connection");
    var connectionstring = "Data Source='MBUDEVQNXT3, 63059';Initial Catalog=ADdash;Integrated Security=SSPI;Provider=SQLOLEDB";
    var strhealthplan = document.getElementById('Healthplan').value;
    var strFromDate = document.getElementById('FromDate').value;
    var strToDate = document.getElementById('ToDate').value;
    
    connection.Open(connectionstring);
    var rs = new ActiveXObject("ADODB.Recordset");
    
    if (document.getElementById('Exceptions').checked == true) {
    rs.Open("SELECT convert(varchar, RunDate, 101) as RunDate, HPID, ClaimID, ClmStatus, AnalystName, AnalystDate, ReviewName, ReviewDate, CorrectName, CorrectDate FROM [ADdash].Report_OK WHERE HPID = '" + strhealthplan + "' and RunDate Between '" + FromDate + "' and '" + ToDate + "' and (ClaimID is not null and ClaimID not IN ('None Found'))", connection);
    } else {
    rs.Open("SELECT convert(varchar, RunDate, 101) as RunDate, HPID, ClaimID, ClmStatus, AnalystName, AnalystDate, ReviewName, ReviewDate, CorrectName, CorrectDate FROM [ADdash].Report_OK WHERE HPID = '" + strhealthplan + "' and RunDate Between '" + FromDate + "' and '" + ToDate + "' ", connection);
    };
    
    if (rs.eof) {
                   document.write("<table>")
                   document.write("<tr>")
                   document.write("<td align='center'><b>No Data Found</b></td>")
                   document.write("<b>No Data Found</b>")
                   document.write("</tr>")
                   document.write("<tr>")
                   document.write("<td colspan='5' align='center'><input type=button value='Back' onclick='history.back();' /></td>")
                   document.write("<input type=button value='Back' onclick='history.back();' />")
                   document.write("</tr>")
                   document.write("</table>")
    } else {
                    document.write("<table width='80%' border='0' cellpadding='0' cellspacing='0' align='center' id='mytable'>")
                    document.write("<tr style='background-color: #87CEFA';>")
                    document.write("<td align='center'><b>Run Date</b></td>")
                    document.write("<td align='center'><b>Health Plan</b></td>")
                    document.write("<td align='center'><b>Claim ID</b></td>")
                    document.write("<td align='center'><b>Status</b></td>")
                    document.write("<td align='center'><b>Analyst Name</b></td>")
                    document.write("<td align='center'><b>Analyst Date</b></td>")
                    document.write("<td align='center'><b>Reviewer Name</b></td>")
                     document.write("<td align='center'><b>Reviewer Dzte</b></td>")
                     document.write("<td align='center'><b>Correction By</b></td>")
                     document.write("<td align='center'><b>Correction Date</b></td>")
                     document.write("</tr>")
    
                     rs.MoveFirst
    
                    while (!rs.eof) {
                    document.write("<tr>")
                    document.write("<td>" + rs.fields(0) + "</td>")
                    document.write("<td>" + rs.fields(1) + "</td>")
                   if (rs.fields(2) == 'None Found') {
                     document.write("<td>" + rs.fields(2) + "</td>")
                   } else {
                    document.write("<td><a href='UpdateOkToPay.html' onclick='open_win();'>" + rs.fields(2) + "</a></td>")
                    }
                     document.write("<td>" + rs.fields(3) + "</td>")
                     document.write("<td>" + rs.fields(4) + "</td>")
                     document.write("<td>" + rs.fields(5) + "</td>")
                     document.write("<td>" + rs.fields(6) + "</td>")
                     document.write("<td>" + rs.fields(7) + "</td>")
                     document.write("<td>" + rs.fields(8) + "</td>")
                     document.write("<td>" + rs.fields(9) + "</td>")
                     document.write("</tr>");
    
                      rs.movenext;
                     }
    
                      document.write("<tr>")
                      document.write("<td colspan='10'>&nbsp;</td>")
                      document.write("</tr>")
                      document.write("<tr>")
                      document.write("<td colspan='5' align='center'><input type=button value='Back' onclick='history.back();' /></td>")
                      document.write("<td colspan='5' align='center'><input type=button value='Export' onclick='write_to_excel();'/></td>")
                      document.write("</tr>")
                      document.write("</table>")
                        }
                      rs.close;
                      connection.close;
                      return true;
                        }
    </script>
    The code resides on my form within a table that I have created.

    I need to know how to make this script display the data back on the same page just below the section where the users enter the criteria.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    use ajax or an iframe, and never use document.write.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Location
    USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not use DOM instead of document.write. Make a DIV with the id of whatever you want and then use document.getElementById(id).innerHTML = "Text Here"; document.write is dead and should never be used

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by lilwayne1556 View Post
    document.write is dead and should never be used
    There is still one instance where document.write can be used - where you want to generate an entire new page fron JavaScript that you want opened in a new window or tab.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    Which means that this could be on case where you might indeed use document.write.

    In this case, you could do the writes into an <iframe>.

    Not ideal, but workable.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The OP didn't tell how that code is called but I can't figure out why that code would open a new tab and generate the output there. I think the output is written on the same page erasing the current content (because of document.write). I assume the code is called onclick of the Search button.

    Why not make a div with id and write the output via innerHTML?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New Coder
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    Which means that this could be on case where you might indeed use document.write.

    In this case, you could do the writes into an <iframe>.

    Not ideal, but workable.
    As long as it's workable then that would be fine.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by felgall View Post
    There is still one instance where document.write can be used - where you want to generate an entire new page fron JavaScript that you want opened in a new window or tab.
    document.write() is the only way to dynamically load blocking external javascript from a non-cors URL. advertisiers use this capability a lot to deliver device and browser-specific packages from a bevy of CDNs. even major sites like NYT use it to selectively show images:

    from http://www.nytimes.com/:
    Code:
    <script type="text/javascript">
                  if (/iPad|iPod|iPhone/.test(navigator.userAgent)){
                    document.write('<img id="mastheadLogo" width="379" height="64" alt="The New York Times" src="http://i1.nyt.com/svg/nytlogo_379x64.svg">');
                  } else {
                    document.write('<img id="mastheadLogo" width="379" height="64" alt="The New York Times" src="http://i1.nyt.com/images/misc/nytlogo379x64.gif">');
                  }
                </script>
    that's in interesting use for it, and i can why why they choose to do it that way for performance reasons. until the <template> tags is well-supported, such hackery will be a routine part of web development.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    document.write() is the only way to dynamically load blocking external javascript from a non-cors URL.
    As long as the JavaScript is attached to the page and references something in the page there is no cross domain issue. There are even scripts you can use (eg. postscribe) that will convert all of the document.write statements so that all the code can be moved to the bottom of the page and still load all the antiquated ads in the right places in the page.

    Using a script like postscribe has the advantages that the JavaScript is all at the bottom of the page where it belongs and any delays in actually loading the ads don't block the visitor from seeing the rest of the page. The disadvantage is that it requires a relatively large additional script to be added to the page (fortunately since the ads are the least important part of the page from your visitor's viewpoint they will perceive pages using it as loading faster even though they are actually slower). You'd get the advantages without the disadvantages if the advertisers stopped using antiquated code - apart from generating entire web pages there has not been any use for document.write since Netscape 4 died.

    If you attach postscribe to a web page then the web page no longer provides any way to access the native document.write call as postscribe replaces that function with its own. So any site providing scripts for others to attach to their page that uses document.write has no guarantee that their code will be run through the native document.write.
    Last edited by felgall; 10-11-2013 at 07:08 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you all for your responses. I am relatively new to this and am not sure how to use a div tag and put the output there. Can someone provide an example of how I would do that exactly?

    I appreciate all the input and assistance you have provided.

    Thanks,
    `John

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by felgall View Post

    If you attach postscribe to a web page then the web page no longer provides any way to access the native document.write call as postscribe replaces that function with its own. So any site providing scripts for others to attach to their page that uses document.write has no guarantee that their code will be run through the native document.write.
    if the postscribe script works, then so would your other script, even if the doc.write was non-native.

    OP: you might want to look into https://github.com/krux/postscribe
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #12
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for your suggestions. The code is called via a submit button on the form. The users inputs their criteria and push the button to submit the query and the code returns the results.

    I am very new to using JavaScript and was wondering if you had an example of how I can place this code in an iframe and write the output via innerHTML.

    ~John

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Give a little more background on what your environment is. Are you using a server-side language (C#, VB.NET, PHP, JSP, etc)? If so, then you don't need javascript. Or are you doing HTML Application (HTA) or a regular HTML that only works in IE with ActiveX enabled and the database resides locally or in the LAN?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #14
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am using regular HTML for IE with Active X enabled, and the database resides on a LAN.

    ~John

  • #15
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Ok. You don't need an iframe. You just have this div on the page:

    PHP Code:
    <div id="output"></div
    Then instead of document.write, you do:

    PHP Code:
    var html '';
    if (
    rs.eof) {
        
    html += '<table><tr><td><b>No Data Found</b></td>........</table>';
    }
    else {
        
    html += '<table width="80%" border="0" cellpadding="0" cellspacing="0" align="center" id="mytable">';
        
    html += '<tr style="background-color: #87CEFA;">';
        
    //...and so on
    }

    //display results
    document.getElementById('output').innerHTML html;
    ... 
    You can make the code cleaner by removing those attributes and instead use CSS to style the table.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    JohnConklin (10-15-2013)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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