I have a form which I load in parts depending on the users selection.
When the users select a date, I search for the qtty of available stock on that date (this are seats on a train travel).
I Have 3 IF()
sentences
The code I have is this:
$.post(url,function(data){
//********************
//data = parseInt(data);
alert("muestra: "+data+".");
//3 opciones
//si viene vacia la respuesta, es por que no encontro, reservas o sea que nadie a reservado para la fecha solicitada
// lo que sigue es crear la reserva de todas formas, luego re-consultar por los valores y traer la cantidad total //podria mejorar
//si viene con 0 es por que no existe disponibilidad para el viaje en esa fecha, tendra que intentar cambiando categoria (vagon) o fecha
//si un numero >=1 hay disponibilidad y se crean los sliders que le permiten seleccionar la cantidad de asientos para reservar
//TODO: cambiar al switch ....
if(data=="false" || data==false){
//if(data=='NaN'){
$("#info-disponibilidad").html("No existen reservas previas, sera el primero en reservar para esta fecha. Asientos Disponibles: ");
$("#info-disponibilidad").css("color","green");
var myRandom=parseInt(Math.random()*99999999); // tecnica cache buster
//url = 'asientos-disponibilidad.php?categoria='+$('#categoria option:selected').val()+'&fecha_reserva='+$('#fecha_reserva').val()+'&rand=' + myRandom;
url = 'crear_reservas.php?viaje='+$('#viajes option:selected').val()+'categoria='+$('#categoria option:selected').val()+'&fecha_reserva='+dateText+'&rand=' + myRandom;
$.post(url,function(data2){
//algo algo con la respuesta... ...
alert("1er: "+data2+".");
});
}
if(!isNaN(data) && data!=0){
//if(!isNaN(data) && data!=0){
alert("2do: "+data);
//este se puede aprender facilmente al buscar el ejemplo en Jquery Ui slider
$("#info-disponibilidad").html("Existen, "+data+" asientos Disponibles");
$("#info-disponibilidad").css("color","green");
//este se puede aprender facilmente al buscar el ejemplo en Jquery Ui slider
$( "#slider-adultos" ).slider({
//range: "min",
min: 1,
max: data,
slide: function( event, ui ) {
$( "#adultos" ).val(ui.value );
$("#adultos").trigger("change");
},
stop: function(event, ui) {
$("#total-box").effect("highlight", {}, 2000);
//var max = parseInt($("#adultos").val());//usar esta variable en lugar de value (por si algo falla)
manejoreservas(ui.value,"ninos", data);
}
});
$("#slider-ninos").slider({
//range: "min",
min: 0,
max: data,
slide: function( event, ui ) {
$( "#ninos" ).val(ui.value );
$( "#ninos" ).trigger("change");
},
stop: function(event, ui) {
$("#total-box").effect("highlight", {}, 2000);
//$("#slider-ninos").slider( "option", "max", max );
//var max = parseInt($("#ninos").val());//
manejoreservas(ui.value,"adultos", data);
}
});
}
else//(!isNaN(data) && data==0 && data!=false)
{ // && data!=false
alert("3ro: "+data+" no disponibilidad");
$("#info-disponibilidad").html("No existen asientos Disponibles");
$("#info-disponibilidad").css("color","red");
}
});
and the php code is this:
<?php
//busca cual es la cantidad de asientos disponibles en una reserva si es que esta existe...
include('conn.php');
if (!$con)
{
die('Fallo conexion a la Base de datos: ' . mysql_error());
}else{
$categoria = $_GET['categoria'];
//$fecha = $_POST['fecha_reserva'];
$fecha = $_GET['fecha_reserva'];
$sql_cant ="SELECT restante FROM reservas WHERE fecha_reserva = '".$fecha."' AND categorias_idcategorias = ".$categoria."";
mysql_select_db("mvargas", $con);
$result = mysql_query($sql_cant);
$num_rows = mysql_num_rows($result);
//echo $sql_cant;
if($num_rows < 0){
echo "";
}else{
//echo "<b>".$num_rows."</b>";
while($row = mysql_fetch_assoc($result)){
$foo = $row['restante'];
$j++;
}
//
//var_dump($foo);
echo $foo;
}
}
?>
The last thing I have achieved, is that everything is working, but it enters the first IF
displaying a message that the user will be first reservation in that date (create the travel) then it enters the third IF
(else in the code shown) erasing the first message, and setting the message for the zero qtty, flashing awkwardly both messages in the screen for the user confusion.
Sorry if it's too long... here is the page for you to see: http://www.micontrol.cl/~mvargas/wordpress/wp-transatacama/reservas-rapidas/form-reservas.php
If I'm understanding you correctly, change the 2nd if to an else if...
....
....
alert("1er: "+data2+".");
});
} else if(!isNaN(data) && data!=0){
//if(!isNaN(data) && data!=0){
alert("2do: "+data);
....
....
It will then only run the 2nd if statement if the first one is not true.
I assume that in the case you are referring to the data
variable is returning 0 or blank. In javascript the statement 0 == false
evaluates to true. You need to use what is called a strict comparison such as 0 === false
which evaluates to false.
So update your code like this:
if(data=="false" || data===false){
it is a good idea to use strict comparison operators at all times unless you have a specific reason not to. They are ===
and !==
Note that the following statements evaluate to true
(false == 0); // true
(false == ""); // true