PHP and AJAX, what am I doing wrong?

Asked

Viewed 56 times

-2

Friends, I am doing an AJAX with PHP but the record is going empty. What I am doing wrong?

function newColor() {
    var nome = document.getElementById("descricao_cor").value;
    var cor = document.getElementById("hexadecimal").value;
    alert (nome);
    alert(cor);
    $.ajax({
        url: "atualiza-cor.php",  
        type: "GET",
        data: { descricao_cor: "nome", hexadecimal: "cor" },   
        cache: false,
        success: function() { 
        document.adm.submit();
        $('#btnSelecionar').trigger('click');
    }
});
}

The Alerts work well. And there’s PHP like this:

//NOVA COR
if (isset($_GET['novaCor']) && ($_GET['novaCor']) != '') {
    echo $sql_novacor = 'INSERT INTO veiculos0_cores (descricao_cor,relevancia,hexadecimal) VALUES("' . $_POST['nome'] . '","", "' . $_POST['cor'] . '")';
    mysql_query($sql_novacor) or die(mysql_error());
}

And a record is entered, but it’s empty. I’m not finding the problem. Someone help me, please?

[]s

  • In PHP you are checking if $_GET['novaCor'] is set, but you are not sending. No Insert vc is putting in value $_POST['name'] and $_POST['color'] but the method you are going through is GET.

1 answer

1


You don’t seem to be using the method POST. Simplify using $.post:

$.post({
    'url': "atualiza-cor.php",
    'data': {
        'descricao_cor': $("#descricao_cor").val(),
        'hexadecimal': $("#hexadecimal").val()
    },
    'success': function() { 
        document.adm.submit();
        $('#btnSelecionar').trigger('click');
    }
});

Remembering: You were returning a string to descricao_cor and hexadecimal. If you take the quotes will return the value that is in the variables descricao_cor and hexadecimal.

  • Thanks Klaider, but what does this data mean->$("#descricao_cor")? Why does it keep inserting empty...

  • It’s almost the same as document.getElementById("descricao_cor").value, but it’s jQuery’s function (you’re using it). Use alert to see what comes out.

  • 1

    Thanks, thank you very much!

Browser other questions tagged

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