No Clear form after wordpress Submit

Asked

Viewed 36 times

-1

built a form that validates within the wordpress function but every time I do Submit it cleans up the whole form and I don’t want it to do that I am sent data by jquery this way

form.submit(function (e) {
        var dados = {
            'action': 'acao_validador',
            'dados': $(form).serialize()
        };
        ajaxscript = { ajax_url: '/wp-admin/admin-ajax.php' };
        $.ajax({
            url: ajaxscript.ajax_url,
            data: dados,
            datatype: 'json',
            method: 'POST',
            success: function (response) {
                if (response.class == 'success') {
                    console.log(response);
                } else {
                    console.log(response);
                }
            },
            error: function (error) {
                console.log(error);
            }
        });
        e.preventDefault();
        return false;
    });

2 answers

0

To resolve this, make clicking on Ubmit call a function that takes the values of the inputs in question in the form before actually sending and then just assign the values that were taken in the function before sending and assign them again at the end

Example I made:

HTML:

<form action="">
  <input type="text" value="" id="txt1">
  <input type="text" value="" id="txt2"> <br> <br>
  <input type="submit" value="Try it" name="" id="btn">
</form>

JAVASCRIPT:

    
    var input1 = document.getElementById('txt1')
    var input2 = document.getElementById('txt2')
    var submit = document.getElementById('btn')

    submit.addEventListener('click', function() {
        var valueInput1 = input1.value
        var valueInput2 = input2.value
        alert('TESTE: Sim foi enviado com o submit e os valores são os mesmos de ok para ver')
    })
    input1.value = valueInput1
    input2.value = valueInput2

Forehead there, and I hope I’ve helped

-1

Just put in the form <form onsubmit="return false">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.