My javascript:
nombre = window.prompt('Escribe el nombre del autor/artista/letrista:','');
if(nombre != ''){
$.ajax({
url: "admin/nuevo_autor.php",
type: "POST",
data: {autor: nombre},
success: function (res) {
alert(res);
if(res=='verdadero'){
var opt = document.createElement('option');
opt.text = nombre;
opt.selected = true;
sel.add(opt);
}
return true;
}
});
}
Calls 'nuevo_autor.php':
<?php
$nuevo = $_POST["autor"];
require("../includes/database.php");
$select = <<<EOT
SELECT * FROM autor
WHERE 1
EOT;
$resultado = $mysqli->query($select);
$res = "verdadero";
for($i=0;$i<$resultado->num_rows; $i++){
$fila = mysqli_fetch_array($resultado, MYSQLI_ASSOC);
if(stristr($fila["autor"], $nuevo)) {
$res = "falso";
break;
}
}
echo json_encode($res);
?>
The Javascript alert shows: FIN (in spanish: END ). Neither ' verdadero ' nor ' falso '. I also tested using the $.get
jQuery function with the same bad result. Where is the error?
stristr($fila["autor"], $nuevo)
will return "0" if $nuevo and $fila['author'] are equal, which in your code will be the equivalent of false
.
Instead you need to explicitly check for the false
value, as in:
if(stristr($fila["autor"], $nuevo) !== false) {
$res = "falso";
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.