Toggle button Save and load value without updating the page

How do I get the code below to be selected on all pages and send the selected value to the database. without refreshing the page.

<html>
        <body>

            <button id="toggle" name="test" onclick="myFunction()">on</button>

            <script>

                function myFunction() {
                    var change = document.getElementById("toggle");
                    if (change.innerHTML == "on")
                    {
                        change.innerHTML = "off";
                    }
                    else {
                        change.innerHTML = "on";
                    }
                }

            </script>
        </body>
    </html>


function update_your_meta_key( $post_id ) {
    if ( isset( $_POST['test'] ) ) {
        update_post_meta( $post_id, 'your_meta_key', $_POST['test'] );
    }

And if it’s not too much to ask to update this plugin or file without reloading the page

http://example.com/wp-content/plugins/test/archivo.php

You can also reload the page when clicked on the toggle button.

More would be better without reloading the page

Since I thank you:)