*controller*
@auth.requires_login()
def view_searches():
    form = SQLFORM(Post)
    if form.process().accepted:
        #pass
        response.flash = 'done'
    if request.vars.message:
        db.post.insert(message = request.vars.message)
        db.commit()
    quiz = db(db.post.author == auth.user.id).select(db.post.ALL,
limitby=(1,0))
    return dict(form =form, quiz=quiz)

N/B
The several functions that will need to use 'quiz' string to process data.
I was posting it to html to view if latest string matches the one on db as
previously described. Otherwise, the functions will pick the string from
the updated db with the latest string as follows:

def Search_name():
  db = current.db
  auth = Auth(db, hmac_key=Auth.get_or_create_key())
  auth.define_tables()
  name  = [r.message for r in db(db.post.author == auth.user.id
).select(db.post.ALL)][-1]
  return name


*the view.html*
See www.sololearn.com from where i borrowed this html code.

{{extend "layout.html"}}

<!DOCTYPE html>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-3.1.1.js";></script>
        <meta name="viewport" content="user-scalable=no, initial-scale=1.0,
maximum-scale=1.0" />
        <title>Page Title</title>
    </head>
    <body>
        <div id="header">Chat Bot</div>
        <div id="messages">
        <a href='https://code.sololearn.com/Wm2n17OoSTDk/?ref=app
'>Original</a>
        </div>
        <form id='user' onsubmit="return false;">
            <input type="text" id="msgbox" name='message' placeholder="Type
a message..." />
            <button id="send"
onclick="ajax('{{=URL('view_searches')}}',['message'],'target');"
>SEND</button>
        </form>
    </body>
</html>

<style>
    * {
       margin: 0px;
       user-select: none;
       -webkit-user-select: none;
       -moz-user-select: none;
    }
    body {
        margin: 0px;
        height:400px;
        bottom:0;
    }
    #user {
        width: 100vw;
        position: fixed;
        left: 0px;
        bottom: 0px;
        display: block;
        background-color: #EEE;
        white-space: nowrap;
        height:105px;
    }
    #msgbox {
       width: calc(80% - 12px);
       min-height: 25px;
       max-height: 35px;
       padding: 5px;
       outline: none;
       border: solid 1px #AAA;
       display: inline-block;
       vertical-align: center;
       float: left;
       background-color: #FFF;
       border-radius: 25px;
       resize: none;
       margin: 0px;
       word-wrap:break-all;
    }
    #send {
       width: 20vw;
       height: 35px;
       display: inline-block
       outline: none;
       border: none;
       color: #FFF;
       background-color: #00F;
       float: left;
       border-radius: 25px;
       padding: 0px;
       cursor: pointer;
       margin: 0px;
    }
    #send:active {
       background-color: #00A;
       outline: none;
    }
    #header {
       display: flex;
       justify-content: left;
       align-items: center;
       width: calc(100% - 30px);
       height: 20px;
       padding: 15px;
       color: #FFF;
       font-size: 200%;
       font-weight: bolder;
       background-color: #00F;
       position: fixed;
       font-family: arial;
    }
    #messages {
       display: block;
       width: 100vw;
       height: calc(100% - 87px);
       background-color: #EEE;
       position: fixed;
       top: 50px;
       left: 0px;
       overflow: auto;
       overflow-x: hidden;
       overflow-y: auto;
    }
    .left {
       text-align: left;
       /*
       display: block;
       */
    }
    .right {
       text-align: right;
       /*
       display: block;
       */
    }
    .incoming {
       background-color: #FFF;
       color: #000;
       border: solid 1px #AAA;
    }
    .outgoing {
       background-color: #00F;
       color: #FFF;
    }
    .section {
       display: block;
       width: calc(100% - 30px);
       padding-left: 15px;
       padding-right: 15px;
       margin-top: 7.5px;
       margin-bottom: 7.5px;
    }
    .message {
       display: inline-flex;
       justify-content: left;
       align-items: center;
       border-radius: 25px;
       padding: 10px;
       font-size: 10pt;
    }
    input:first {
       color: #F00;
    }
    .incoming:active {
       background-color: #EEE;
    }
    .outgoing:active {
       background-color: #00A;
    }
    * {
       outline: none;
       tap-highlight: none;
       -webkit-tap-highlight: none;
       -webkit-tap-highlight-color: none;
       -moz-tap-highlight: none;
       -moz-tap-highlight-color: none;
       -khtml-tap-highlight: none;
       -khtml-tap-highlight-color: none;
    }
    a {
       display: block;
       text-align: center;
    }
</style>

<script>
    $(function(){
        var commands = {
           "random":"var rand = (Math.floor(Math.random() * 10));
incoming(rand);",
           "commands":"var objstr = JSON.stringify(commands); objstr =
JSON.stringify(commands, null, 4); incoming(objstr)"
        }
        var responseSys = {
                "hi":"Hello There!",
                "bye":"GoodBye!"
                };
                function cmd(name, action){
                   commands[name] = action;
                }
        function outgoing(text){
            var newMsg = "<div class='section right'><div class='message
outgoing'>" + text + "</div></div>";
            $("#messages").append(newMsg);
        }
        function incoming(text){
         var newMsg = "<div class='section left'><div class='message
incoming'>" + text + "</div></div>";
            $("#messages").append(newMsg);
            window.scrollTo(0, parseInt($("#messages").innerHeight))
        }
        $("#send").click(function(){
            $("#msgbox").trigger("blur")
            var text = $("#msgbox").val();
            if(text != null && text != ""){
            $("#msgbox").val("");
            text = text.replace(/</ig, "&lt;");
            text = text.replace(/>/ig, "&gt;");
            text = text.replace(/\n/ig, "<br />");
            outgoing(text);
            reply(text)
            }
            else{
               // Praise the Sun
               incoming("Please, don't send empty messages.")
            }
        });
        $("#msgbox").keyup(function(e){
            if(e.which == 13){
               $("#send").trigger("click")
            }
            else{
               // Do Nothing
            }
        });

        incoming("Hello!<br />This is a small ChatBot made by Andrew
Grider. Feel Free to implement any commands or functions but please don't
change this message! If you do add to the code however, then feel free to
add your name to the contributors message!");
        incoming("Add responses using this JavaScript function,
responses(your_message, my_reply) and add commands using this function,
cmd(command_name, command_action)");
        incoming("Contributors:<br />Maz<br />The Coding Sloth")
        function responses(msg, response){
        msg = msg.toLowerCase();
            responseSys[msg] = response;
        }
        function reply(txt){
        txt = txt.toLowerCase();
        if(txt[0] == "r" && txt[1] == "e" && txt[2] == "s" && txt[3] == "p"
&& txt[4] == "o" && txt[5] == "n" && txt[6] == "s" && txt[7] == "e" &&
txt[8] == "s" && txt[9] == "("){
                try{
                    eval(txt);
                }
                catch(e){
                   incoming(e);
                }
            }
            else if(responseSys[txt] != undefined && responseSys[txt] !=
null && responseSys[txt] != "" ){
               incoming(responseSys[txt]);

            }
            else if(commands[txt] != null && commands[txt] != undefined &&
commands[txt] != ""){
               try{
                   try{
                       eval(commands[txt])
                   }
                   catch(e){
                       incoming("Error Executing")
                   }
               }
               catch(e){
                  incoming("Command not defined")
               }
            }
            else if(txt[0] == "c" && txt[1] == "m" && txt[2] == "d"){
               try{
                  eval(txt)
               }
               catch(e){
                  incoming(e) ;
               }
            }
            else setTimeout(function () {
                return incoming('Just a sec...');
                }, 1000);
            return setTimeout(function () {
                return incoming('{{=quiz}}');
                }, 2000);
        }

        responses("example", "My Response")
        responses("hello", "Hello World.. or.. person.")
    });
</script>

Much appreciated for your constant support. I wont ever forget!
Kind regards
Maurice.


On Wed, Nov 15, 2017 at 5:26 PM, Anthony <abasta...@gmail.com> wrote:

>
> controller/default/index.py
>> ...
>> form =SQLFORM(Post, formstyle='table3cols')
>> if request.vars.message:
>>     db.post.insert(message=request.vars.message)
>>     pass
>> String = ''
>> for s in db(db.post.author==auth.user.id).select(db.post.ALL):
>>     String = s.message
>>
>
> First, you don't need to loop through all the records just to get the last
> one -- instead, just index with [-1]. Or better yet, have the db sort in
> reverse order and use limitby=(0, 1) to get the last record.
>
> Anyway, in the code above, the final value of String should be the message
> from the record inserted above. Perhaps the error is somewhere in the code
> you haven't shown (e.g., the view code).
>
> Also, not sure what you mean by "refreshing the database".
>
> Anthony
>
> --
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "web2py-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/web2py/ERqZpKfZgyc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> web2py+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to