guides

How to create a Svelte form

In this post we’re going to look at how to hook up FormBackend with a form in Svelte.

Create a new Svelte app

We’re going to start from scratch and create a new Svelte app. If you already have one you can skip this step and go to the next one.

Go ahead and run the following in your terminal

npm create svelte@latest formbackend-svelte

We picked the skeleton app, which will just give us a barebones project structure. We’re using formbackend-svelte as our directory name but you can of course use whatever!

Next go in to the directory you just created and run npm install like so

cd formbackend-svelte
npm install

We can now go ahead and start the dev server by running shell npm run dev -- --open

and you should see this when visiting http://localhost:5173:

Visiting the Svelte development server URL in your browser

Create a contact form in FormBackend

Log in to your FormBackend account and visit the forms index page. Go ahead and create a new form and give it a name you can remember it by.

After your form has been created, you’ll see the “Submissions” page which is where new submissions will appear. If you navigate to the “Set up” page you can see the unique URL for your form. We’ll copy that!

Create a new contact us page in the Svelte app

Svelte’s routing system is based on the file system, so any directory you create inside src/routes will be the relative path of your site. So src/routes/contact will be accessible on http://localhost:5173/contact.

Go ahead and create a new directory inside of src/routes named contact and inside that we’ll create a file named +page.svelte with the following content (full path src/routes/contact/+page.svelte):

<h1>Contact us</h1>

<form action="https://www.formbackend.com/f/b1a2be3a2e0521ff" method="POST">
  <div>
    <label for="name">Name</label>
    <input type="text" id="name" name="name" required>
  </div>

  <div>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>
  </div>

  <div>
    <label for="message">Message</label>
    <textarea type="email" id="email" name="email" required></textarea>
  </div>

  <button type="submit">Submit</button>
</form>

You’ll now have a simple unstyled form that looks like this

The contact us page with the contact form

We can make the form look a little better by providing our own CSS. Let’s add the following at the bottom of src/routes/contact/+page.svelte

<style>
  h1 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 2rem;
    font-weight: 600;
    margin-bottom: 1rem;
  }
  .fieldset + .fieldset,
  form + form {
    margin-top: 8px;
  }

  label {
    font-family: Arial, Helvetica, sans-serif;
    color: #334155;
    display: block;
    font-size: .8rem;
    font-weight: bold;
    text-transform: uppercase;
  }

  input,
  textarea,
  select {
    border: 1px solid #ddd;
    color: #475569;
    font-size: 1rem;
    padding: 5px;
    border-radius: 4px;
  }

  button[ type="submit"] {
    background: purple;
    background: #14b8a6;
    border: none;
    box-shadow: none;
    color: white;
    border-radius: 2px;
    font-size: .8rem;
    text-transform: uppercase;
    font-weight: 500;
    padding: 8px 12px;
    margin-top: 16px;
  }
</style>

We now have a styled form that looks like this

The Svelte contact form styled with CSS

After filling it out and clicking the submit button, you’ll be redirected to FormBackend’s submission success page and if you navigate to the Submissions page for the form you created in FormBackend you should see the submission you just added.

Handling submissions with JavaScript

Svelte makes it easy to handle form submissions without a page redirect. You can use Svelte’s on:submit|preventDefault and fetch to POST to FormBackend’s AJAX endpoint, then reactively show a success message. This gives your users a smooth, single-page experience.

What to configure next

Your Svelte form is live. Here’s what you can set up in FormBackend:

Guides for other frameworks: React, Vue.js, Next.js, Astro, Hugo, and more.