Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery if() works bad asking for empty ajax response
    primarykey
    data
    text
    <p>I have a form which I load in parts depending on the users selection.</p> <p>When the users select a date, I search for the qtty of available stock on that date (this are seats on a train travel).</p> <p>I Have 3 <code>IF()</code> sentences </p> <ul> <li>First, I ask if the db response doesn't return any row (so the travel for that date haven't been created so I must create it in that moment for the costumer to reserve)</li> <li>Second, I ask if the response is a number (I only return a number, sorry if I didn't specified that before) AND if it's bigger than zero. so I update the available qtty. So the costumer can't over buy non-existing seat.</li> <li>Third and last, if the others fails and the qtty is zero, I just send the customer a message indicating that, the travel is full for that date, he/she must choose a different date to travel.</li> </ul> <p>The code I have is this:</p> <pre><code>$.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 &gt;=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()+'&amp;fecha_reserva='+$('#fecha_reserva').val()+'&amp;rand=' + myRandom; url = 'crear_reservas.php?viaje='+$('#viajes option:selected').val()+'categoria='+$('#categoria option:selected').val()+'&amp;fecha_reserva='+dateText+'&amp;rand=' + myRandom; $.post(url,function(data2){ //algo algo con la respuesta... ... alert("1er: "+data2+"."); }); } if(!isNaN(data) &amp;&amp; data!=0){ //if(!isNaN(data) &amp;&amp; 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) &amp;&amp; data==0 &amp;&amp; data!=false) { // &amp;&amp; data!=false alert("3ro: "+data+" no disponibilidad"); $("#info-disponibilidad").html("No existen asientos Disponibles"); $("#info-disponibilidad").css("color","red"); } }); </code></pre> <p>and the php code is this:</p> <pre><code>&lt;?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 &lt; 0){ echo ""; }else{ //echo "&lt;b&gt;".$num_rows."&lt;/b&gt;"; while($row = mysql_fetch_assoc($result)){ $foo = $row['restante']; $j++; } // //var_dump($foo); echo $foo; } } ?&gt; </code></pre> <p>The last thing I have achieved, is that everything is working, but it enters the first <code>IF</code> displaying a message that the user will be first reservation in that date (create the travel) then it enters the third <code>IF</code> (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.</p> <p>Sorry if it's too long... here is the page for you to see: <a href="http://www.micontrol.cl/~mvargas/wordpress/wp-transatacama/reservas-rapidas/form-reservas.php" rel="nofollow">http://www.micontrol.cl/~mvargas/wordpress/wp-transatacama/reservas-rapidas/form-reservas.php</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload