skip to Main Content

at the moment I’m using submit input type inside html. But it cause every time a reload of the whole page, when I hit the button. So because of it, I want to use JavaScript now, so my page doesnt reload every time. My problem: I dont know how, and hope someone will help. It’s also possible to use jQuery?
This is my actual html button:

<html>
    <body>   
        <table class="buttons">
            <tr>
                <td>
                    <form method="post">
                        <p> <input name='"Variable".startstop' type="hidden"> </p>
                        <p> <input class="Start"value="Start" type="submit"> </p>
                    </form>
                </td>
                <td>
                    <form method="post">
                        <p> <input name='"Variable".startstop'value="0" type="hidden"> </p>
                        <p> <input class="Stop" value="Stop" type="submit"> </p>
                    </form>
                </td>               
            </tr>
        </table>
    </body>
  </html>

"Variable.startstop" is inside my plc and I would like to chage it from 1 to 0 and 0 to 1

2

Answers


  1. add id to your form and catch submit

    <form id="target">
    
    $( "#target" ).submit(function( event ) {
          console.log( "Handler for .submit() called." );
          event.preventDefault();
    });
    
    Login or Signup to reply.
  2. To be specific AJAX is the solution to it, we can also use Fetch API for that. I would recommend using fetch. Before that change, your type="Submit" to "type=button".

    document.getElementById('submit').addEventListener('click', () => {
      // Here put all your data of the form 
      const data = {};
      // Here is the call to your endpoint
      fetch('https://example.com/profile', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
      })
      .then(response => response.json())
      .then(data => {
        // Here is the response form the server
        console.log('Success:', data);
      })
      .catch((error) => {
        // Error if any
        console.error('Error:', error);
      });
    })
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search