Ajax without sending the action

Asked

Viewed 51 times

1

Good afternoon, I have this ajax code where I use to update my form, it happens that when I give the Submit it goes to the php page, and I do not want it, I just want the return of it.

follows the code:

function alterProduct(obj){
    var form = $(obj);
    var dados = new FormData(obj);
    $.ajax({
        url: form.attr('action'),
        type: form.attr('method'),
        data: dados,
        processData: false,             
        cache: false,
        contentType: false,
        success: function( data ) { 
            if ( data == 'OK' ) {
                alert('Dados enviados com sucesso');
            } else {
                alert(data);
            }
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });

    return false;   
}

my form:

<form method="post" enctype="multipart/form-data" class="update-product" onsubmit="alterProduct(this)" action="ajax/update_product.php">
    <input type="text" name="id" value="<?php echo $a->current()->id; ?>" hidden>
    <div class="form-group">
        <label>Nome do Produto</label>
        <input type="text" name="name" value="<?php echo $a->current()->name; ?>" class="form-control" placeholder="Digite o nome do seu produto">
    </div>
    <div class="form-group">
        <label>Preço</label>
        <input type="text" name="price" value="<?php echo $a->current()->price; ?>" class="form-control money" placeholder="Digite o valor do seu produto">
    </div>
    <div class="form-group">
        <label>Quantidade</label>
        <input type="number" name="qtd" class="form-control" value="<?php echo $a->current()->qtd; ?>" placeholder="Digite a quantidade a ser cadastrada">
    </div>
    <input type="submit" name="update" class="btn-action" value="Alterar">
    <button type="button" class="btn-outline" data-dismiss="modal">Fechar</button>
</form>

1 answer

5


Because of how you are calling the function, need one more return outside:

... onsubmit="return alterProduct(this)" ...
  • Thanks for the help.

Browser other questions tagged

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