skip to Main Content

I am using AJAX to automatically update text inputs and textareas and save them to the database.

Everything works until there are two of form the same form. I figured out the AJAX function is taking the values from the form. I checked with console.log(data). I wonder if the last form is overriding everything.

It also works perfectly fine if all names are unique. However, my SQL code will have so much repetition and it won’t be efficient if the user makes 10 of these forms.

How can I make it so this doesn’t happen?

Thank you!

index.php


<form method="post" class="form-input">
    <input type="hidden" name="position" value="1" class="position">
    <table>
        <tbody>
            <tr>
                <td class="th">Offer Heading</td>
                <td><input name="heading" class="text-input" type="text"></td>
            </tr>
            <tr>
                <td class="th">Description</td>
                <td><textarea name="description"></textarea></td>
            </tr>
            <tr>
                <td class="th">Call-To-Action</td>
                <td><input name="call_to_action" class="text-input" type="text"></td>
            </tr>
        </tbody>
    </table>
</form>

script.js

    var timeoutId;
    $('textarea, .text-input').on('input propertychange change', function() {
        console.log('Textarea Change');

        clearTimeout(timeoutId);
        timeoutId = setTimeout(function() {
            // Runs 1 second (1000 ms) after the last change
            saveToDB();
        }, 1000);
    });
    
    function saveToDB() {
        console.log('Saving to the db');
        var data = $(".form-input").serialize();
        console.log(data);
        $.ajax({
            url: "",
            type: "POST",
            async: true,
            cache: false,
            data: data,
            success: function(data){
                console.log('completed');
            }
        });
    }
    
    $('.form-input').submit(function(e) {
        saveToDB();
        e.preventDefault();
    });

Thank you!

2

Answers


  1. Make difference between two ajax request for each form. E.g. use data attribute in forms and check it on sending ajax request.

    Login or Signup to reply.
  2. Here is a snippet that demonstrates some of the points made in the comments to the question. One further change I made is that I am no longer using the global variable timeoutId to store the timeout handle. Instead I store this information in a .timeoutId-property of the current form. This will prevent the error of not storing the data from one form if a text field in another form is changed directly afterwards.

    $('textarea, .text-input').on('input propertychange change', function() {
        const frm=$(this).closest("form")[0];
        console.log(frm.name,'Textarea Change');
    
        clearTimeout(frm.timeoutId);
        frm.timeoutId = setTimeout(function() {
            // Runs 3 seconds (3000 ms) after the last change
            saveToDB(frm);
        }, 3000);
    });
    
    function saveToDB(frm) {
        console.log(frm.name,'Saving to the db');
        var data = $(frm).serialize();
        console.log(data);
        /* $.ajax({
            url: "",
            type: "POST",
            async: true,
            cache: false,
            data: data,
            success: function(data){
                console.log('completed');
            }
        });*/
    }
    
    $('.form-input').submit(function(e) {
        saveToDB(this);
        e.preventDefault();
    });
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <form method="post" class="form-input" name="one">
    <input type="hidden" name="position" value="1" class="position">
    <table>
        <tbody>
            <tr>
                <td class="th">Offer Heading</td>
                <td><input name="heading" class="text-input" type="text"></td>
            </tr>
            <tr>
                <td class="th">Description</td>
                <td><textarea name="description"></textarea></td>
            </tr>
            <tr>
                <td class="th">Call-To-Action</td>
                <td><input name="call_to_action" class="text-input" type="text"></td>
            </tr>
        </tbody>
    </table>
    </form><hr>
    <form method="post" class="form-input" name="two">
    <input type="hidden" name="position" value="1" class="position">
    <table>
        <tbody>
            <tr>
                <td class="th">Offer Heading</td>
                <td><input name="heading" class="text-input" type="text"></td>
            </tr>
            <tr>
                <td class="th">Description</td>
                <td><textarea name="description"></textarea></td>
            </tr>
            <tr>
                <td class="th">Call-To-Action</td>
                <td><input name="call_to_action" class="text-input" type="text"></td>
            </tr>
        </tbody>
    </table>
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search