0
Good morning everyone... I need a help... I need to return success or error values via ajax. But it is not returning anything. Follow the codes:
HTML
<div class="form-group col-md-4 col-xs-12">
<input type="text" class="form-control input-lg" id="cep" name="ceps" placeholder="_____-___">
</div>
<button type="button" class="btn btn-warning botao" id="buscar">Calcular</button>
<div id="resultad"></div>
JS
$('#buscar').click(function(){
$.ajax({
type:'POST',
url:'/inc-busca-cep.php',
dataType: 'html',
data:{
cep: $('cep').val()
},
success: function(data){
if (data){
$('#resultad').html('<span class="text-danger">Não entregamos neste cep</span>');
} else {
$('#resultad').html('<span class="text-danger">Entregamos neste cep. Frete Grátis!</span>');
}
},
});
return false;
});
PHP
$pdo = db_connect();
$cep = isset($_POST['cep']) ? $_POST['cep'] : null;
function limpa($valor){ $valor = trim($valor); $valor = str_replace("-", "", $valor); return $valor;}
$cepLimpo = limpa($cep);
$stmt = $pdo->prepare("SELECT * FROM ceps WHERE :a BETWEEN cep_de AND cep_ate");
$stmt->bindParam(':a', $cepLimpo);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);
if($result):
echo "ok";
else:
echo "erro";
endif;
Because there must be a syntax error in JS and you didn’t see it. Review your condition inside the callback
success
; there is aif
which does not, in fact, have a.– Woss
I understood, but I don’t know how to do :( I’m learning ajax yet.... I only know how to use if in php
– Betinho Silva
This is Javascript syntax, has no relation to AJAX and the syntax is the same as PHP. If you don’t know this yet, I recommend that you study at least the basics of Javascript before trying to do something more advanced: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
– Woss
@Andersoncarloswoss now understood what you meant... to do the same thing I did in if in PHP...
– Betinho Silva
@Andersoncarloswoss made the adjustment in JS IF, but even so does not return me anything...
– Betinho Silva