0

I'm very new to JavaScript and jQuery, trying to find a less verbose way to submit a form to Electron, JSON format is fine it can be handled on the server-side. How would you rewrite this code to make it more concise?

<section>
  <input id="server"        type="text"     placeholder="Server">
  <input id="instanceName"  type="text"     placeholder="Instance Name">
  <input id="linkedServer"  type="text"     placeholder="Linked Server">
  <input id="histDatabase"  type="text"     placeholder="Historical Database">
  <input id="modelDatabase" type="text"     placeholder="Model Database">
  <input id="schema"        type="text"     placeholder="Schema">
  <input id="username"      type="text"     placeholder="Username">
  <input id="password"      type="password" placeholder="Password">
  <input id="clientCode"    type="text"     placeholder="Client Code">
<footer>
  <button class="submit">Create Account</button>
</footer>
$("#modal-custom").on('click', '.submit', function(event) {
    let server        = document.getElementById('server').value;
    let instanceName  = document.getElementById('instanceName').value;
    let histDatabase  = document.getElementById('histDatabase').value;
    let linkedServer  = document.getElementById('linkedServer').value;
    let modelDatabase = document.getElementById('modelDatabase').value;
    let schema        = document.getElementById('schema').value;
    let username      = document.getElementById('username').value;
    let password      = document.getElementById('password').value;
    let clientCode    = document.getElementById('clientCode').value;
    ipcRenderer.send('update:databaseDetails', server, instanceName, 
    linkedServer, histDatabase, modelDatabase,schema, username, 
    password, clientCode)
});
2
  • 1
    Instead of document.getElementById('server').value; , you can do $('#server').val(); Commented Mar 10, 2020 at 4:23
  • Thanks, do I still have to go through the process of defining id's and selecting them via jQuery? Or is there a way to submit the form as JSON or as an array? Commented Mar 10, 2020 at 4:28

1 Answer 1

1

The in the tag you can specify a method and an action.

method: specifies how to send form-data --> "GET" or "POST".

action: specifies the URL route where this data will be handled.

    <form action="/formsubmit" method="POST">
      <input id="server"        type="text"     name="server">
      <input id="instanceName"  type="text"     name="instance">
      <input id="linkedServer"  type="text"     name="linked">
    </form>

Server-side you will receive the user inputs as JSON data with all the input values filled in the request.body;

Using the tag's "name" as the key and the user input as the value:

{
    server: <user's input>,
    instance: <user's input>,
    linked: <user's input>,
}

You can read more on HTML forms here: https://www.w3schools.com/tags/att_form_method.asp

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.