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?
– Sergio