I'm trying to make data records by ajax
and the data is not going to BD
.
I did a direct test between html and php and it worked, but html
, ajax
and php
will not.
HTML:
<form method="post">
AVERAGE MONTHLY SALE Sale S / Margin
Follow 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!'));
}