Deskpro Developer Starter
  • Introduction
    • Introduction
  • Embeddables
    • Website Widget
    • Form Embed
  • API Basics
    • API Introduction
    • Auth
      • API Keys
      • API Tokens
        • OAuth
        • Token Exchange Endpoint
      • Access Control
    • API Reference
    • Request Format
    • Batch Requests
    • Sideloading
    • SDKs
  • Getting Started with the API
    • PHP
      • Initialize the client
      • Add a form
      • Create an article
      • Tickets
      • Methods
    • JavaScript
      • Initialize the client
      • Add a form
      • Create an article
      • Methods
Powered by GitBook
On this page
  1. Getting Started with the API
  2. JavaScript

Add a form

Next, let's add a form to the page and get ready to handle it.

<!DOCTYPE>
<html>
  <head>
    <script src="https://unpkg.com/@deskpro/deskpro-api-client-javascript@2.0.0/dist/index.js"></script>
  </head>
  <body>
    <form>
        <div>
            Title:
            <input type="text" name="title">
        </div>
        <div>
            Content:
            <textarea name="content" cols="80" rows="10"></textarea>
        </div>
        <button type="submit">Submit</button>
    </form>
    <script>
        // Listen for the form submit event. We will use an ajax
        // request to send the new article to the API.
        var form = document.getElementsByTagName('form')[0];
        form.addEventListener('submit', function(e) {
            e.preventDefault();
            var body = {
                title: form.elements['title'].value,
                content: form.elements['content'].value
            };

            console.log(body);
        });
    </script>
  </body>
</html>

Open the index.html in your browser and open the debug console. Fill out and submit the form. You'll see a bunch of JSON output. Something like:

{
    "title": "This is a title",
    "content": "This is the content."
}
PreviousInitialize the clientNextCreate an article

Last updated 6 years ago