Example file
07-fill-your-own-form.html
Your own quote form, filled in live as the visitor talks to the chat (onApplicationUpdate), with a log of each change.
To try this page, download it, replace the placeholders and put it on a test page of your website. Opening the file from your computer won't work, because the embeds check the website address they're on.
<!doctype html>
<!--
Your own quote form, filled in from the chat. The visitor can type in the
form, or tell the agent instead: as the agent records each detail, the
webchat calls onApplicationUpdate with the whole application, and this page
copies it into its fields. A field the visitor is typing in is left alone,
and each field the chat fills flashes. The panel underneath shows the
application's status, its price once there is one, and a log of each change.
Things to try: say "I'd like a quote" in the chat and answer the agent's
questions. Reload the page part-way through and open the chat again: the
form fills itself back in.
Replace YOUR_AGENT_ID below, and change the keys in ANSWER_FIELDS to your
journey's question keys. The agent's Web channel must be switched on, or the
chat stays hidden.
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Webchat: fill your own form</title>
<style>
body { font-family: system-ui, sans-serif; max-width: 720px; margin: 2rem auto; padding: 0 16px; }
form { display: grid; gap: 12px; }
label { display: grid; gap: 4px; font-weight: 600; }
input, select { font: inherit; padding: 8px; border: 1px solid #9ca3af; border-radius: 6px; }
/* A field the chat just filled. */
.from-chat { animation: from-chat 1.6s ease-out; }
@keyframes from-chat { from { background: #fde68a; } to { background: transparent; } }
#summary { margin-top: 24px; padding: 12px 16px; background: #f3f4f6; border-radius: 8px; }
#changes { font-family: ui-monospace, monospace; font-size: 13px; }
</style>
</head>
<body>
<h1>Get a life insurance quote</h1>
<p>Fill in the form, or ask our assistant in the corner and it will fill the form in for you.</p>
<form id="quote-form">
<label>First name <input name="first_name" autocomplete="given-name"></label>
<label>Last name <input name="last_name" autocomplete="family-name"></label>
<label>Email <input name="email" type="email" autocomplete="email"></label>
<label>Phone <input name="phone_number" type="tel" autocomplete="tel"></label>
<label>Date of birth <input name="dob" type="date" autocomplete="bday"></label>
<label>Postcode <input name="postal_code" autocomplete="postal-code"></label>
<label>Cover amount (£) <input name="cover_pounds" type="number" min="0" step="1000"></label>
<label>Term (years) <input name="term_years" type="number" min="1" max="50"></label>
<label>Do you smoke?
<select name="smoker">
<option value=""></option>
<option value="true">Yes</option>
<option value="false">No</option>
</select>
</label>
<label>Height (cm) <input name="height_cm" type="number"></label>
<button type="submit">Get my price</button>
</form>
<div id="summary">
<p><strong>Status:</strong> <span id="status">no application yet</span></p>
<p><strong>Price:</strong> <span id="price">none yet</span></p>
<h2>Changes</h2>
<ol id="changes"></ol>
</div>
<script src="https://js.subcontext.com/webchat-latest.js" crossorigin="anonymous"></script>
<script>
var form = document.getElementById('quote-form')
// Your journey's question keys (the keys in the application's risk_factors),
// and the form field each answer goes in. Change these to your own.
var ANSWER_FIELDS = {
smoker: 'smoker',
height_cm: 'height_cm'
}
// The person's details: the record's customer fields, and the form field for each.
var CUSTOMER_FIELDS = ['first_name', 'last_name', 'email', 'phone_number', 'dob', 'postal_code']
// Put a value in a field, unless the visitor is typing in it right now.
// Flash the field when its value changes.
function fill(name, value) {
var field = form.elements[name]
if (!field || value === undefined || value === null) return
if (field === document.activeElement) return
var text = String(value)
if (field.value === text) return
field.value = text
field.classList.remove('from-chat')
void field.offsetWidth // restart the flash
field.classList.add('from-chat')
}
function money(pence) {
return '£' + (pence / 100).toLocaleString('en-GB', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
}
function logChange(change) {
var item = document.createElement('li')
item.textContent = new Date().toLocaleTimeString() + ' ' +
(change.initial ? 'everything so far' : 'from ' + (change.source || 'Subcontext')) + ': ' +
(change.changed.join(', ') || 'nothing yet')
document.getElementById('changes').prepend(item)
}
SubcontextWebchat.init({
// Your agent's id: in the dashboard, go to Agents and open the agent. The id is in the
// address bar, and in the snippet under Channels, Web, Configure.
agentId: 'YOUR_AGENT_ID',
// Called with the whole application each time it gains or changes information:
// first with everything so far, then after each change.
onApplicationUpdate: function (application, change) {
CUSTOMER_FIELDS.forEach(function (name) { fill(name, application.customer[name]) })
// Cover is in pence on the application; this form asks in pounds.
if (application.cover_amount_pence !== null) fill('cover_pounds', application.cover_amount_pence / 100)
fill('term_years', application.term_years)
Object.keys(ANSWER_FIELDS).forEach(function (key) {
fill(ANSWER_FIELDS[key], application.risk_factors[key])
})
document.getElementById('status').textContent = application.status
if (application.quote) {
document.getElementById('price').textContent =
money(application.quote.total_premium_monthly_pence) + ' a month (' + application.quote.reference + ')'
}
if (application.policy) {
document.getElementById('price').textContent += ', policy ' + application.policy.policy_number
}
logChange(change)
}
})
// Your form's own submit goes to your own systems, as it does today.
form.addEventListener('submit', function (event) {
event.preventDefault()
document.getElementById('status').textContent = 'on your website, the form is sent here as it always has been'
})
</script>
</body>
</html>