Update database via input using ajax/php

Asked

Viewed 614 times

3

I need to update a table in the database, reading the data entered in the input, by clicking the button. This is what I have so far:

HTML:

<td>ADSL</td>

<td class="superdanger"> <input type="number"  id="vmadsl" placeholder="<?php echo "Valor atual: (Acima de ".$placeholder[0]."%)"; ?>"></input> </td>           

<td class="superwarning"> <input type="number"  id="aadsl" placeholder="<?php echo "Valor atual: (Acima de ".$placeholder[1]."%)"; ?>"></input> </td>

<td class="success"> <input type="number"  id="vdadsl" placeholder="<?php echo "Valor atual: (Acima de ".$placeholder[2]."%)"; ?>"></input> </td>


                <td> <button type="submit" id="teste"/</button> </td>

AJAX SCRIPT:

<script>
$(function(){
        $('#teste').click(function(){

            $.ajax({
            type      : 'post',

            url       : 'capacidade/relatorios/ricardo/matriz_capacidadericardo/attbutttonadsl.php',

            data      :  {vermelho: $('#vmadsl').val(), amarelo: $('#aadsl').val()},

            success: function (response) {
            alert('Alterado com sucesso');
            },
            error: function () {
            alert('Erro');
            }
        });

    });
});
</script>

PHP:

<?php
    include 'capacidade/background_bd_conexao.php';

    $vermelhoadsl = $_POST["vermelho"];
    $amareloadsl = $_POST["amarelo"];

    mysql_query(" UPDATE valor FROM matriz_coloracao WHERE nome = 'Vermelho ADSL' SET valor = '".$vermelhoadsl."' ");
    mysql_query(" UPDATE valor FROM matriz_coloracao WHERE nome = 'Amarelo ADSL' SET valor = '".$amarelogadsl."' ");
?>
  • Not that it has anything to do with the problem but it has an error in HTML: it must be <button type="submit" id="teste">BLABLAH</button>

  • 1

    What mistake are you making?

  • can be sending the form, try to cancel the event by clicking #test button

  • He is not changing anything in the bank, the error seems to me to be that he does not take the values of "red" and "yellow". Thanks @Miguel

  • The warning "Successfully altered" is printed.

2 answers

-1

Change the way you are sending the data in the ajax request.

Below is an example.

$(document).ready(function() {
    $('#teste').click(function(){

        $.ajax({
        'type' : 'POST',

        'url'       : 'capacidade/relatorios/ricardo/matriz_capacidadericardo/attbutttonadsl.php',

        'data'      :  'vermelho=' + $('#vmadsl').val() + '&amarelo=' + $('#aadsl').val(),

        'success' : function (response) {
            alert('Alterado com sucesso');
        },
        error: function () {
        alert('Erro');
        }
    });

});

-2

Insert your inputs into a formdata to send in the "ajax date" field.

$(document).ready(function() {
    $('#teste').click(function(){
        var fd = new FormData();
        fd.appen('vermelho',$('#vmadsl').val());
        fd.appen('amarelo',$('#aadsl').val());
        $.ajax({
        'type' : 'POST',

        'url'       : 'capacidade/relatorios/ricardo/matriz_capacidadericardo/attbutttonadsl.php',

        'data'      :  fd,

        'success' : function (response) {
            alert('Alterado com sucesso');
        },
        error: function () {
        alert('Erro');
        }
    });

});

Browser other questions tagged

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