简体   繁体   中英

Prevent function loading again in form when submit

I have a dynamic table created through a function tablekrw() in a form by JavaScript. This table loads some default values and creates 200 empty table rows on my page. When submit, the table values are stored in a database. But now, when I press submit, I got twice the values which are in the table.

My minimal HTML code:

if (isset($_POST["submit"])) {
        $insert->Insert();
<form>
<table id="table1" class="table table-sw table-bordered">
                <tr id="tablehead">
                    <th>Sw</th>
                    <th>Krw</th>
                </tr>
                <tbody id="tablekrw">
                <script>
                    tableKrw();
                </script><
                /tbody>
                <tbody>
                <tr><td><label></label></td></tr>
                <tr><td><button type="button" id="empty1">Empty Sw Krw table</button></td></tr>
                </tbody>
            </table>

<tr>
    <td>
        <input class="btn btn-primary" type="submit" value="Submit" name="submit">
    </td>
</tr>
</form>

Function:

function tableKrw() {
$('document').ready(function (e) {
    $('#tablekrw').append('<table></table>');
    var tr = $('#tablekrw').children();

    tr.append('<tr><td><input type="text" class="td-sw" name="sw1[]" value="0.15" id="default-td">' +
        '</td><td><input class="td-sw" type="text" name="krw[]" value="0.0"></td> </tr>');
for (let i = 0; i < 200; i++) {
        tr.append('<tr> <td><input type="text" class="td-sw" name="sw1[]" id="default-td">' +
            '</td><td><input class="td-sw" type="text" name="krw[]"></td> </tr>');
    }

The standard values 0.15 and 0.0 are now stored in the database as: 0.15 0.0 0.15 0.0

How can I prevent to store the values twice? I tried with Preventdefault, but that didn't work.

Insert() code:

$this->lastId = $db->lastInsertId();

        $sql = "insert into unsteady_state_table_krw (sw1, krw, us_id) values (:sw1, :krw, :us_id)";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':sw1', $this->sw1);
        $stmt->bindParam(':krw', $this->krw);
        $stmt->bindParam(':us_id', $this->lastId);
        foreach ($_POST['sw1'] as $key => $value) {
            if (!empty($_POST['sw1'][$key]) && ($_POST['krw'][$key])) {
                $this->sw1 = $_POST['sw1'][$key];
                $this->krw = $_POST['krw'][$key];
                $stmt->execute();

Seems you are creating your rows each time DOM is ready. You can check that by storing a value in sessionStorage or localStorage when the job has been done, for example before appending all your rows again :

if (typeof localStorage.rowsAlreadyCreated === 'undefined') {
    tableKrw();
    localStorage.rowsAlreadyCreated = 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM