DEV Community

Geoff
Geoff

Posted on

create a form with svelte tutorial

Benefits of Using a Form Backend With Svelte

When building a website, handling form submissions can be a hassle if you don’t want to deal with backend development, databases, or email handling directly. Using a form backend service like Fabform.io simplifies this process by providing:

  1. No Backend Required: You don’t need to set up a server or backend to manage form submissions.
  2. Secure and Reliable: Fabform handles all the data processing, ensuring secure and reliable submissions.
  3. Easy Integrations: You can easily integrate with services like Google Sheets, Zapier, or send custom emails with minimal effort.
  4. Simple Setup: With just a URL and a simple form structure, you can be up and running in minutes.

Now that you understand the advantages of using a form backend, let’s see how to integrate Fabform.io in a Svelte app to handle your form submissions.


Step 1: Set Up Your Svelte Project

  1. Create a new Svelte project by running:
   npx degit sveltejs/template svelte-fabform
   cd svelte-fabform
   npm install
   npm run dev
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Form on Fabform

  1. Go to Fabform.io and sign up or log in.
  2. Create a new form on the Fabform dashboard. Fabform will provide an action URL for your form, which will look like:
   https://fabform.io/f/your-form-id
Enter fullscreen mode Exit fullscreen mode

Step 3: Integrate the Form in Svelte

Now that you have the Fabform action URL, integrate the form into your Svelte app by replacing the form’s action attribute with the URL provided by Fabform.

Here’s the Svelte form code:

<script>
  let formData = {
    name: '',
    email: '',
    message: ''
  };
</script>

<main>
  <h1>Contact Form</h1>

  <form 
    action="https://fabform.io/f/your-form-id" <!-- Replace with your Fabform URL -->
    method="POST"
    on:submit|preventDefault>

    <!-- Optional: Redirect URL after submission -->
    <input type="hidden" name="redirect_to" value="https://your-redirect-url.com" />

    <!-- Name Field -->
    <div>
      <label for="name">Name</label>
      <input 
        type="text" 
        id="name" 
        name="name" 
        bind:value={formData.name} 
        required 
      />
    </div>

    <!-- Email Field -->
    <div>
      <label for="email">Email</label>
      <input 
        type="email" 
        id="email" 
        name="email" 
        bind:value={formData.email} 
        required 
      />
    </div>

    <!-- Message Field -->
    <div>
      <label for="message">Message</label>
      <textarea 
        id="message" 
        name="message" 
        bind:value={formData.message} 
        required
      ></textarea>
    </div>

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

<style>
  main {
    padding: 20px;
    font-family: Arial, sans-serif;
  }

  form {
    max-width: 500px;
    margin: 0 auto;
  }

  div {
    margin-bottom: 10px;
  }

  label {
    display: block;
    margin-bottom: 5px;
  }

  input, textarea {
    width: 100%;
    padding: 8px;
    font-size: 16px;
  }

  button {
    padding: 10px 15px;
    font-size: 16px;
    background-color: #4CAF50;
    color: white;
    border: none;
    cursor: pointer;
  }

  button:hover {
    background-color: #45a049;
  }
</style>
Enter fullscreen mode Exit fullscreen mode

Key Details:

  • Action URL: The form's action URL should be replaced with the URL from Fabform. This is where your form data will be submitted.
  • Form Fields: The name attributes of the fields (name, email, message) should match the field names configured in your Fabform form.
  • Optional Redirect: You can include a hidden redirect_to field to define where users are sent after the form is submitted.

Step 4: Test the Form

  1. Run the Svelte app:
   npm run dev
Enter fullscreen mode Exit fullscreen mode
  1. Open the browser and navigate to http://localhost:5000.
  2. Fill out the form and submit. Fabform will handle the submission and either redirect the user to the specified URL or show a confirmation page.

Using Fabform.io provides a simple, secure way to handle form submissions without needing to manage a backend. By embedding Fabform's form action URL into your Svelte app, you can easily collect and manage form submissions. Plus, Fabform’s integrations with services like Google Sheets and email makes it even more powerful for handling the data.

Top comments (0)