Ajax without sending action [duplicate]

1

Good afternoon, I have this ajax code where I use to do an update on my form, it happens that when I submit it, it goes to the php page, and I do not want it, I just want the feedback.

follow 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>
    
asked by anonymous 16.08.2017 / 17:25

1 answer

5

On account of how you are calling the function, you need another return on the outside:

... onsubmit="return alterProduct(this)" ...
    
16.08.2017 / 17:42