Thanks!
I did exact;y what you said. It worked so well without the js. And am able
to get the lest field item posted.
Any idea why the js code below does not work?.
Because once I use it, that's when i don't get the last posted message.I
used the latest web2py.
Regards
<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}}');
                }, 5000);
        }

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



On Fri, Nov 17, 2017 at 6:18 PM, Anthony <abasta...@gmail.com> wrote:

> On Wednesday, November 15, 2017 at 9:46:34 AM UTC-5, Maurice Waka wrote:
>>
>> *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()
>>
>
> First, by default, form.process() will itself insert a record in the
> database, so you should not subsequently do this manual insert, as you will
> get two copies of the same data in the database. Second, there is no reason
> to call db.commit() here -- the entire request is wrapped in a transaction,
> and any inserts made within the transaction will be immediately visible to
> subsequent queries within the same request. The danger of calling
> db.commit() is that if there is a subsequent error in the request handling,
> the insert will not be rolled back by the framework.
>
>
>>     quiz = db(db.post.author == auth.user.id).select(db.post.ALL,
>> limitby=(1,0))
>>
>
> What records do you want in quiz? limitby=(1, 0) will actually give you
> all but the first record (and because you don't specify an orderby, the
> exact order of records isn't even guaranteed). If you want the latest
> record, do something like:
>
> quiz = db(db.post.author == auth.user.id).select(db.post.ALL, orderby=~db.
> post.created_on, limitby=(0, 1))
>
> The does a descending sort on created_on and then selects the first
> record, which will be the most recently created.
>
>
>> def Search_name():
>>   db = current.db
>>   auth = Auth(db, hmac_key=Auth.get_or_create_key())
>>   auth.define_tables()
>>
>
> I assume this function is in a module and imported into a controller or
> model in the app. If so, there is no reason to redefine auth and its
> tables. Simple add auth to the current object, or even better, pass the
> auth object to this function directly.
>
>
>>   name  = [r.message for r in db(db.post.author == auth.user.id
>> ).select(db.post.ALL)][-1]
>>
>
> The above is an inefficient way to get the last message, as you are
> unnecessarily retrieving all the records from the database, then iterating
> through all of them, and only then selecting the last one. Instead, use the
> code shown above to get only the most recent record.
>
> 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