How to get webpage to send info in json format to doctype like web form

how to get webpage to send info in json format to doctype. I want it to be something of how the web forms do it except I want it to be on a web page

Create a web page under app/www.

Ref:

Hi

Create the web page from website module.

Add below code in main section of your webpage.

<form class="form-inline">
<!--First Name-->
<div class="form-group">
<label class="sr-only" for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="First Name" required>
</div>
<!--Last Name-->
<div class="form-group">
<label class="sr-only" for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Last Name" required>
</div>
<!--Submit Button-->
<button type="submit" class="btn btn-default">Submit</button>
</form>

Add below code in script:

$(document).ready(function() {
     $('form').on('submit', function(event) {
		frappe.call({
			method: "yourcustomapp.api.get_from_data", // write a function in api file
			args:{
				'fname': $('#firstName').val(),
				'lname': $('#lastName').val(),
			},
			callback: function(r) {
				frappe.msgprint("Your Message")
				$('#firstName').val('');
				$('#lastName').val('');
			}
		});
      event.preventDefault();
      });
});

In api.py file,

from __future__ import unicode_literals
import frappe

@frappe.whitelist()
def get_from_data(fname,lname,gnore_permissions=True):
	data = frappe.new_doc("Lead")
	data.lead_name = fname + lname
	data.save()
	
	frappe.db.commit()

Above code will create the new Lead in database.

1 Like