简体   繁体   English

通过使用web2py中的JavaScript将元素存储在数据库中

[英]storing elements in a database by using JavaScript in web2py

The first thing that I would like to point out is that I am like a nervous programmer or nervous learner. 我想指出的第一件事是,我就像一个紧张的程序员或紧张的学习者。 I feel like I want to learn things fast, so I start checking websites and books very quickly and If I feel like what I am reading doesn't satisfy my need then I just close it and start doing something else. 我觉得我想快速学习,所以我开始非常快速地检查网站和书籍,如果我觉得所阅读的内容不能满足我的需求,那么我就关闭它并开始做其他事情。 I am just wondering if that kind of behavior sounds familiar to you. 我只是想知道您是否熟悉这种行为。

I have the following problem. 我有以下问题。 I am just starting to learn how to program in web2py and I need to store javascript input in a sqlite database, so for example I have the following table: 我刚刚开始学习如何在web2py中编程,我需要将javascript输入存储在sqlite数据库中,因此,例如,我有下表:

db = DAL ('sqlite://storage.sqlite')
db.define_table('company', Field('name', notnull = True, unique = True), format = '%(name)s')
db.define_table(
   'contact',
   Field('name', notnull = True),
   format = '%(name)s'
)

and I have the following script code

li.innerHTML = document.getElementById('task').value;

so I need to store that document.getElementById in the database. 所以我需要将该document.getElementById存储在数据库中。 Can u point out the topics that I need to study in order to accomplish such task? 您能指出我完成该任务需要学习的主题吗?

Start by reading the jQuery and Ajax chapter of the documentation. 首先阅读文档的jQuery和Ajax章节。 In particular, check out the ajax function . 特别要检查ajax函数

If the task element is a form element with a "name" attribute, you can call the ajax function as follows (assuming the name of the form element is "task"): 如果task元素是具有“ name”属性的表单元素,则可以按以下方式调用ajax函数(假设form元素的名称为“ task”):

ajax("{{=URL('default', 'insert_task')}}", ['task']);

If the task element is not a form element, you can instead pass it to web2py via the query string: 如果task元素不是form元素,则可以通过查询字符串将其传递到web2py:

ajax("{{=URL('default', 'insert_task')}}" + '?task=' + encodeURIComponent(li.innerHTML));

Then in the web2py controller, the insert_task action would handle the insert: 然后在web2py控制器中, insert_task操作将处理插入操作:

def insert_task():
    db.mytable.insert(task=request.vars.task)
    return 'Success'

Note, the ajax function takes a third argument, which can be (a) the id of an HTML element where the returned value should be inserted, (b) a Javascript function to which the returned value will be passed, or (c) the string ":eval", which will result in the returned value being interpreted as Javascript and evaluated. 请注意, ajax函数采用第三个参数,它可以是(a)应在其中插入返回值的HTML元素的id ,(b)返回值将传递到的Javascript函数,或(c)字符串“:eval”,这将导致返回的值被解释为Javascript并进行评估。 You can use this argument if you want to display some message on the page or make some change to the display after the insert has completed. 如果要在页面上显示一些消息或在插入完成后对显示进行一些更改,则可以使用此参数。

Finally, if the ajax function is not adequate for your needs, you can always use jQuery's Ajax facilities, or any other Javascript method of making Ajax calls. 最后,如果ajax函数不足以满足您的需求,则可以始终使用jQuery's Ajax工具或进行Ajax调用的任何其他Javascript方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM