0

How can I use a function after using fetch with a return?

On the onchange event, I've placed an alert but that doesn't run. After the page is refreshed with fetch.

<select name="Request_keuzen" id="request_keuzen" class="form form-control" onchange="update_request_keuze_1(this.value)">
                        <option value="<?php echo $request_keuze_1 ?>"><?php echo $request_keuze_1 ?></option>
                        <?php
                        while ($row5 = mysqli_fetch_assoc($result5)):; ?>
                            <option value="<?php echo $row5['naam']; ?>"><?php echo $row5['naam']; ?></option>
<?php endwhile; ?>
</select>
function iso_nummer_content(isonumer) {

        const data = new FormData();

        let lisl_lijst_nummer = document.getElementById("lisl_lijst_nummer").value;

        data.set("lisl_lijst_nummer", lisl_lijst_nummer);
        data.set("isonummer", isonumer);

        fetch("Fill_lisl_list2_items.php", {method: 'POST', body: data})
            .then(response => response.text())
            .then(tekst => document.getElementById("scriptophalen").innerHTML = tekst);
    }
function update_request_keuze_1(str) {

        const data = new FormData();

        let selection = str;
        let id = document.getElementById("id").value;

        data.set("request_keuze_1", selection);
        data.set("id", id);

        fetch("Update_lisl_list_item_request_keuze_1.php", {method: 'POST', body: data})
    }

Expected results:

After using fetch and my page is reloaded I can still use my function update_request_keuze_1.

FIXED WITH:

 function update_request_keuze_1() {

        var table = document.getElementById('tabel_lisl_lijst_fase_2');

        for (var i = 1; i < table.rows.length; i++) {
            table.rows[i].onclick = function () {
                let id = this.cells[1].innerHTML;
                let selection = this.cells[18].children[0].value;

                const data = new FormData();

                data.set("request_keuze_1", selection);
                data.set("id", id);

                fetch("Update_lisl_list_item_request_keuze_1.php", {method: 'POST', body: data})
            }
        }
    }
Rob
  • 176
  • 1
  • 14
  • 1
    Possible duplicate of [Can scripts be inserted with innerHTML?](https://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml) – Gonzalo Apr 26 '19 at 08:31
  • 1
    Your `fetch` call in the last snippet has no `.then` attached - so you're doing nothing with the response. – Robin Zigmond Apr 26 '19 at 08:40
  • 1
    I would suggest either using php OR fetch to get data in your html file, but using fetch to update values inserted by php is convoluted and hard to debug. – Kokodoko Apr 26 '19 at 08:43
  • @RobinZigmond the last snippet is only for a page that's runs an update SQL script so no response is needed – Rob Apr 26 '19 at 08:44

0 Answers0