How do you send data to CouchDB with or without JavaScript

I have a show that displays a form with fields filled out from a document. I would like to change the values ​​in the field and then save the updated document.

I am having trouble finding a clear and concise example of how to do this.

Seriously, just ending this example, works wonders for so many people (I'm going to leave a lot of material to make it concise).

Install couchapp

This is beyond the scope of my question, but here is the completeness.

Create Kuchapping

Again, this is well beyond my question. Here is a short tutorial on how to create a couchapp.

Create Template

Create a folder in the root of your template called couchapp. Inside the templates folder, create an HTML page called myname.html. Insert the following into it.

<html> <head> <title>{{ title }}</title> </head> <body> <form method='post' action='#'> <fieldset> Hello <input type='text' name='name' value='{{ name }}'> <input type='submit' name='submit' value='submit'> </form> </body> </html> 

Create show

See the tutorial above for hwo for this.

Add this code to the show called myname.

 function(doc, req) { if (doc) { var ddoc = this var Mustache = require("vendor/couchapp/lib/mustache"); var data = { title: "The Name", name: "Bobbert" } return Mustache.to_html(ddoc.templates.myname, data) } else { return ('nothing here baby') } } 

Refresh document with new name ...

So, who can complete this step on both the client side and the server side?

Please do not direct me to the guide, I need to read it in your words.

Thanks.

Edit:

Although the return value is not very pretty, just submit the form to the update handler by updating the document.

+3
source share
1 answer

You probably want to learn update handler functions .

The update processor handles granular document transformations. Thus, you can take 1 form, which has one specific purpose, and only update the corresponding fields in the document through the update handler.

Your update handler will have to accept the PUT request from your form. The browser cannot do this directly, so you will need javascript to handle this for you. If you use jQuery, this plugin can take your form and submit it without problems via AJAX, using PUT for you.

Inside the function, you can accept the fields that you accept, in this case name and apply them directly to the document. (input validation can be done using validate_doc_update function)

Update handler (in your project document)

 { "updates": { "name": function (doc, req) { doc.name = req.form.name; return [doc, "Name has been updated"]; } } } 

HTML

<form id="myForm" action="/db/_design/ddoc/_update/name/doc_id">...</form>

Javascript

 $(document).ready(function() { $('#myForm').ajaxForm({ type: "PUT", success: function () { alert("Thank you"); } }); }); 

Once you have included this basic example and started, adding a few extra features to the update handlers is not that difficult. :)

+5
source

Source: https://habr.com/ru/post/893680/


All Articles