I have an ajax script which goes and queries a mysql DB in a php, if there is a record I return an echo "yes"; and if not "no";
here the file delete_radio_per_serial.php
<?php
//Configuracion de la conexion a base de datos
$link = mysql_connect('localhost', 'wiilson', 'Wilson.456*') or die('No se pudo conectar: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db('sqlcomunicaciones') or die('No se pudo seleccionar la base de datos');
//variables POST
$serial=$_POST['serial'];
$total = mysql_num_rows(mysql_query("SELECT * FROM dispositivos WHERE serie ='$serial'"));
if($total==0)
{
echo "no";
}
else
{
$sql=mysql_query("DELETE FROM dispositivos WHERE serie ='$serial'");
echo "si";
}
// Liberar resultados
mysql_free_result($sql);
// Cerrar la conexión
mysql_close($link);
?>
This or not I keep it in a variable resp where I want to evaluate if it is a yes to do a few things later or if it is a no also, the problem is that it does not enter the if, if I print the answer in an alert before to enter the if, if it prints well, I get it yes or no, but the alert (which test use) inside the if, does not execute them
here the ajax script
function objetoAjax(){
var xmlhttp=false;
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
if(!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
//Función para recoger los datos del formulario y enviarlos por post
function continuar_eliminar()
{
var serial_digitado=document.getElementById('input_serial').value;
if(serial_digitado=="")
{
alert('Debe digitar el serial del radio a eliminar');
location.reload(true);
}
else
{
var desicion = confirm("¿Esta seguro?");
if (desicion == true)
{
var serial=document.getElementById("input_serial").value;
}
else
{
location.reload(true);
}
}
//div donde se mostrará lo resultados
divResultado = document.getElementById('resultado');
//instanciamos el objetoAjax
ajax=objetoAjax();
//uso del medotod POST
//archivo que realizará la operacion
//eliminar_radio_por_serial.php
ajax.open("POST", "eliminar_radio_por_serial.php",true);
//cuando el objeto XMLHttpRequest cambia de estado, la función se inicia
ajax.onreadystatechange=function()
{
if (ajax.readyState==4) {
//mostrar resultados en esta capa
divResultado.innerHTML = ajax.responseText
//guardo si o no aqui en la variable resp
var resp=ajax.responseText;
// como prueba, imprimo el resultado y si funciona efectivamente
alert(resp);
if(resp=="si") //aqui ya probé con ===
{
//aqui al if donde no entra
alert('si hay registros');
}
if(resp=="no")
{
//a este tampoco
alert('no hay registros');
}
}
}
//LimpiarCampos();
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//enviando los valores a registro.php para que inserte los datos
ajax.send("serial="+serial)
}