No response for ajax reauest
Posted: 2023-06-02 00:53
Hi
Trying to follow the course instructions, (Udemy) I wrote the following script, which works as expected, that is, it returns (displays) the number of reservations made at the requested site and date.
<?php
$currDir = dirname(__FILE__) . '/..';
include("$currDir/defaultLang.php");
include("$currDir/language.php");
include("$currDir/lib.php");
/* Grant access a usuarios que pueden acceder */
$od_from = get_sql_from('Reservas');
if (!$od_from){
header('HTTP /1.0 401 ACCESO NO AUTORIZADO!');
exit;
}
// echo "Acces granted!<br>";
$idSitio = intval($_REQUEST['idSitio']);
$enFecha = $_REQUEST['enFecha'];
$nSitios = sqlValue("SELECT COUNT(*) FROM Reservas WHERE Sitio ='{$idSitio}' AND Fecha='{$enFecha}'");
// Now, $nSitios contains the number of reservations for this date and Sitio
echo $nSitios;
?>
</code>
If I call the script "manually" in my browser
http://localhost/cczres/hooks/ajax-vali ... 2023/05/31
it shows 1, or 0 , as appropriate. Until here, no problem.
But, my ajax request
/* Esta funcion muestra un error y selecciona el campo donde ocurre */
function show_error(field, msg){
modal_window({
message: '<div class="alert alert-danger">' + msg + '</div>',
title: 'Error in ' + field,
close: function(){
$j('#' + field).focus();
$j('#' + field).parents('.form-group').addClass('has-error');
}
});
return false;
}
/* Transforma el input del usuario en un objeto Date de JS */
function get_date(date_field){
var y = $j('#' + date_field).val();
var m = $j('#' + date_field + '-mm').val();
var d = $j('#' + date_field + '-dd').val();
var date_object = new Date(y, m - 1, d);
if(!y) return false;
return date_object;
}
/* jscript to validate */
$j(function(){
$j('#update, #insert').click(function(){
// Fecha must be a date not before today when the user insert or update the reserva
// Get Sitio from input form
var enSitio = $j('#Sitio').val();
var numSitio = Number(enSitio);
// Get date from input form
var y = $j('#Fecha').val();
var m = $j('#Fecha-mm').val();
var d = $j('#Fecha-dd').val();
// and format
if (m < 10){
m = '0' + m;
}
if (d < 10){
d = '0' + d;
}
strDate = y + '/' + m + '/' + d;
console.log(strDate);
debugger;
if (!y){
return show_error('Fecha', 'Fecha Incorrecta!'); // This is only to follow as in the course
}
else {
$j.ajax({
url: 'hooks/ajax-validaSitio.php',
data: { NumSitio: NumSitio, enFecha: strDate},
success: function(data){
$j('#Comentarios').val('Answer is ' + data);
console.log(data);
debugger;
}
});
}
});
})
And, no matter I do, I never get an "success" answer from my $j.ajax request
All the code inside the function(data){ ...} Never is executed
What am I doing wrong ??
And, how can I debug java script & php code??
Trying to follow the course instructions, (Udemy) I wrote the following script, which works as expected, that is, it returns (displays) the number of reservations made at the requested site and date.
<?php
$currDir = dirname(__FILE__) . '/..';
include("$currDir/defaultLang.php");
include("$currDir/language.php");
include("$currDir/lib.php");
/* Grant access a usuarios que pueden acceder */
$od_from = get_sql_from('Reservas');
if (!$od_from){
header('HTTP /1.0 401 ACCESO NO AUTORIZADO!');
exit;
}
// echo "Acces granted!<br>";
$idSitio = intval($_REQUEST['idSitio']);
$enFecha = $_REQUEST['enFecha'];
$nSitios = sqlValue("SELECT COUNT(*) FROM Reservas WHERE Sitio ='{$idSitio}' AND Fecha='{$enFecha}'");
// Now, $nSitios contains the number of reservations for this date and Sitio
echo $nSitios;
?>
</code>
If I call the script "manually" in my browser
http://localhost/cczres/hooks/ajax-vali ... 2023/05/31
it shows 1, or 0 , as appropriate. Until here, no problem.
But, my ajax request
/* Esta funcion muestra un error y selecciona el campo donde ocurre */
function show_error(field, msg){
modal_window({
message: '<div class="alert alert-danger">' + msg + '</div>',
title: 'Error in ' + field,
close: function(){
$j('#' + field).focus();
$j('#' + field).parents('.form-group').addClass('has-error');
}
});
return false;
}
/* Transforma el input del usuario en un objeto Date de JS */
function get_date(date_field){
var y = $j('#' + date_field).val();
var m = $j('#' + date_field + '-mm').val();
var d = $j('#' + date_field + '-dd').val();
var date_object = new Date(y, m - 1, d);
if(!y) return false;
return date_object;
}
/* jscript to validate */
$j(function(){
$j('#update, #insert').click(function(){
// Fecha must be a date not before today when the user insert or update the reserva
// Get Sitio from input form
var enSitio = $j('#Sitio').val();
var numSitio = Number(enSitio);
// Get date from input form
var y = $j('#Fecha').val();
var m = $j('#Fecha-mm').val();
var d = $j('#Fecha-dd').val();
// and format
if (m < 10){
m = '0' + m;
}
if (d < 10){
d = '0' + d;
}
strDate = y + '/' + m + '/' + d;
console.log(strDate);
debugger;
if (!y){
return show_error('Fecha', 'Fecha Incorrecta!'); // This is only to follow as in the course
}
else {
$j.ajax({
url: 'hooks/ajax-validaSitio.php',
data: { NumSitio: NumSitio, enFecha: strDate},
success: function(data){
$j('#Comentarios').val('Answer is ' + data);
console.log(data);
debugger;
}
});
}
});
})
And, no matter I do, I never get an "success" answer from my $j.ajax request
All the code inside the function(data){ ...} Never is executed
What am I doing wrong ??
And, how can I debug java script & php code??