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.
Results 1 to 6 of 6
  1. #1
    SxN
    SxN is offline
    New Coder
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Getting results from previous query instead of actual

    Hi All,

    I am trying to talk to a database from a dynamic web application.

    I came up with the following template:

    The page that is loaded looks like this:
    Code:
    <html>
    <frameset rows="100%,0%" framespacing="0">
    <frame name="FC256" frameborder=0 marginwidth=0 marginheight=0 src="a1.html" scrolling="auto">
    <frame name="query" scrolling=no src="query.html">
    </frameset>
    </html>
    The first frame uses all the available space and contains my real web page. The second frame is "hidden" and helps me to post queries and get results.

    query.html looks like this:
    Code:
    <html>
    <body>
    <form id=sqlQ action="/cgi-bin/query.py" method=post>
    <input id=qr name=qr>
    <input id=rw name=rw>
    </form>
    </body>
    </html>
    a1.html (main code, in the first frame) begins like this:
    Code:
    <head>
    <script type="text/javascript" src="query.js"></script>
    <script type="text/javascript">
    ... my stuff here ...
    and the included query.js is the following one:
    Code:
    var rez = None;
    var done = false;
    
    function Query(qr,rw)
    { top.query.document.getElementById('qr').value = qr;
      top.query.document.getElementById('rw').value = rw;
      top.query.document.getElementById('sqlQ').submit();
      while (!done);
      done = false;
      if (rez == '-')
      { alert("EXCEPTION: database misbehaviour");
        rez = new Array();
      }
      else
      { if (rez.indexOf('^') > -1)
        { r1 = rez.split('^');
    	  rez = new Array();
    	  for(k=0; k<r1.length; k++)
    	    rez[k] = r1[k].split('`');
    	}
    	else if (rez.indexOf('`') > -1)
    	{ r1 = rez.split('`');
    	  rez = new Array();
    	  rez[0] = r1;
    	}
    	else rez = new Array();
      }
    }
    It consists basically of two variables and a function. rez is where query results will end up, done is used to put query.js on hold until an answer is available.

    On the server side there is a query.py:
    Code:
    #!/usr/bin/env python
    
    import cgi, cgitb
    import MySQLdb
    
    f= cgi.FieldStorage()
    
    rw = f.getvalue('rw')
    qr = f.getvalue('qr')
    
    c = MySQLdb.connect(user="userName", passwd="passWord", db="dataBase")
    cc = c.cursor()
    
    try:
      rz = cc.execute(qr)
      if rw:
        rz = ''
        while 1:
          r = cc.fetchone()
          if r == None:
            if rz: rz = rz[1:]
            break
          rz += '^'+'`'.join(['%s' % i for i in r])
    except:
      rz = '-'
    
    print '''Content-type: text/html
    
    <html>
    <head>
    <script type="text/javascript">
    function Results()
    { top.FC256.rez = %s;
      top.FC256.done = true;
    }
    </script>
    </head>
    <body onLoad="Results()">
    <form id=sqlQ action="/cgi-bin/query.py" method=post>
    <input id=qr name=qr>
    <input id=rw name=rw>
    </form>
    </body>
    </html>''' % rz
    
    cc.close()
    c.close()
    The Python code is getting the query string from the qr <input> from query.html's form, and a flag for r/o or r/w from the other field form. After querying MySQL it replaces the old query.html with a version that includes a little Javascript function which is storing the answer in rez and then setting done to true to release the waiting query.js.

    The results are communicated as a character string, with backstick as field separator, and carret as record separator, and formatted as an Array on the client side.

    Queries look like:
    Code:
    Query("SELECT so-and-so FROM so-and-so WHERE ...",1);
    Query("INSERT INTO such-and-such ...",0);
    Query("DELETE FROM ....",0);
    The first query doesn't give nothing (if I inspect rez and done they come back undefined. As a consequence, the while(!done); loop never ends. The browser offers me to stop manually the script.). The second query gives me the results of the first one. The third the results of the second, the forth the results of the third...

    What am I doing wrong?

    I have full access to all the resources: servers, config files etc. I run on latest FireFox/Windows 7.

    Thanks for your advice,
    SxN

  • #2
    SxN
    SxN is offline
    New Coder
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    149 views, not a suggestion... am I doing it so fundamentally wrong? or the answer is so obvious that I should figure it out?

    Any pointers are welcome.

    Thanks,
    SxN

  • #3
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I suppose everyone is just too happy they never have to touch a frameset again in their life to get their hands dirty on your code.

    So, you're trying to halt code execution until the request is through, and you're doing that with
    PHP Code:
    while (!done); 
    That's never going to work, since it will completely lock up the Javascript interpreter. Some time during that loop, the request will have finished, but there's no way to let the Javascript interpreter know, because it's busy.

    After manually stopping the script, the interpreter becomes idle, so it can finally do other stuff, like setting done to true. Then you fire off the second request, and since done is now true, the loop will be skipped completely. The second request hasn't had any time yet to come back, so rez still holds the data from the first request, so that's what you're going to see then.

    So much for the major flaw. I won't go into any details on how to fix your code, because I'm pretty happy we've finally escaped the dark ages of the web too, and don't want to touch your ancient code either.

    You posted this in the AJAX section of the forum, so why don't you just ditch that whole frameset approch and go for an AJAX solution instead?
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #4
    SxN
    SxN is offline
    New Coder
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks venegal for your reply.

    I don't know much about this things and I hoped to find inspiration on the net.

    Is AJAX built in browsers like JavaScript? Or needs to be installed?

    Can you point me to a tutorial or some examples? I'm willing to learn :-)

    Thanks,
    SxN

  • #5
    SxN
    SxN is offline
    New Coder
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I found all the answers at

    http://www.xul.fr/en-xml-ajax.html

    Saturday I'll have the opportunity to test the new approach.

    Thanks for the tip.
    SxN

  • #6
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    AJAX is, despite its fancy name, just another thing you can do with Javascript, and can be used in all browsers. See here for a bunch of articles: https://developer.mozilla.org/en/AJAX
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback


  •  

    Posting Permissions

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