data registration via ajax

Asked

Viewed 837 times

0

I’m trying to register data by ajax and the data is not going to the BD. I did a test, direct, between html and php and it worked, but html, ajax and php won’t. HTML:

<form method="post">

MONTHLY AVERAGE SALE Sale S/Margin

Follows the code JQ with ajax:

$(document).ready( function(){

    $("#salvar").click( function(){

        var dataString = $("form").serialize();

        $.ajax({
            url: 'php/salvarCustos.php',
            type: 'POST',
            dataType: 'json',
            data: dataString,

            success: function(data){
                alert(JSON.stringify(data));
                if(data.status == 1){
                    $("#msg").val(data.status);
                    $("#msg").show();
                }
                if(data.status == 2){
                    $("#msg").val(data.status);
                    $("#msg").show();
                }
            },

            error: function(data){
                alert(data);
            }
        });
    });
})

PHP:

@$vendaMediaMensal = $_POST['vendaMediaMensal'];
@$vendaSemMargemPercentual = $_POST['vendaSemMargemPercentual'];
$vendaMediaMensal = floatval(str_replace(',', '.', str_replace('.', '', $vendaMediaMensal)));
$vendaSemMargemPercentual = floatval(str_replace(',', '.', str_replace('.', '', $vendaSemMargemPercentual)));
$qryConsulta = mysql_query("SELECT * FROM custos");
$qryNum = mysql_num_rows($qryConsulta);

if($qryNum == 0){
    $qryInsere = "INSERT INTO custos VALUES('$vendaMediaMensal','$vendaSemMargemPercentual')";
    $insere = mysql_query($qryInsere);
    echo json_encode( array('status' => 1, 'msg' => 'Cadastro efeutado com sucesso!'));
}else{
    $qryAtualiza = "UPDATE custos SET vendaMediaMensal='$vendaMediaMensal'";
    $atualiza = mysql_query($qryAtualiza);
    echo json_encode( array('status' => 2, 'msg' => 'Atualização efeutada com sucesso!'));
}
  • Can you put HTML in too? There is an error in the console?

2 answers

1

I see a problem in the query. Missing are the fields where these values will be inserted.

Must be:

"INSERT INTO custos (`campo1`,`campo2`) VALUES ('$vendaMediaMensal','$vendaSemMargemPercentual')";
  • @Sergio, no use, do not put the table fields and always worked. Use this same SQL command line always and works. I think the problem is in JQ, because I tested HTML with PHP and the registration worked.

  • @Gustavosevero the answer is Lelo Legend and not mine. You can put the HTML as I asked in the comment in the question?

0

This should be pq vc is setting up your ajax to send json, only with the code below:

var dataString = $("form").serialize();

... vc is sending "application/x-www-form-urlencoded" (in terms of formatting is the same that we find in the querystring of a url).

You should pass an object (or JSON) in the "date attribute":

data: { atributo: "Valor" }

And in PHP you could take the data like this:

$body = json_decode(file_get_contents('php://input'));
echo $body->atributo;

Or... You can simply leave everything as it is and remove the attribute "dataType". In this case it will work like Submit without ajax.

Browser other questions tagged

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