undefined index for PHP-Collection of common programming errors
hello people i am doing a function with calendar using AJAX and PHP
$showmonth = $_POST['showmonth'];
$showyear = $_POST['showyear'];
$pre_days = date ('w', mktime(0,0,0, $showmonth, 1, $showyear));
$post_days = (6 - (date('w', mktime(0, 0, 0, $showmonth, $day_count, $showyear))));
I keep getting error messages saying:
Notice: Undefined index: showmonth in C:\xampp\htdocs\calendar_start.php on line 2
Notice: Undefined index: showyear in C:\xampp\htdocs\calendar_start.php on line 3
Warning: cal_days_in_month() expects parameter 2 to be long, string given in C:\xampp\htdocs\calendar_start.php on line 8
Warning: mktime() expects parameter 4 to be long, string given in C:\xampp\htdocs\calendar_start.php on line 9
Warning: mktime() expects parameter 4 to be long, string given in C:\xampp\htdocs\calendar_start.php on line 10
I think it has something to do with my AJAX java script but i just dont know what there more parts to the AJAX code but i think this is the part troubling me
function next_month() {
var nextmonth = showmonth + 1;
if(nextmonth >12) {
nextmonth = 1;
showyear = showyear + 1;
}
showmonth = nextmonth;
var hr = new XMLHttpRequest();
var url = "calendar_start.php";
var vars = "showmonth="+showmonth+"&showyear="+showyear;
hr.open("POST",url,true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("showCalendar").innerHML = return_data;
}
}
hr.send(vars);
document.getElementById("showCalendar").innerHTML = "processing...";
}
this maybe the problem too:
function initialCalendar() {
var hr = new XMLHttpRequest();
var url = "calendar_start.php";
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
showmonth = month;
showyear = year;
var vars = "showmonth="+showmonth+"&showyear="+showyear;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("showCalendar").innerHML = return_data;
}
}
hr.send(vars);
document.getElementById("showCalendar").innerHTML = "processing...";
}
-
Are you sending and receiving parameters using same method(POST/GET)…? if not so..it may give undefined index error… i.e. if your sending parameters using GET and receiving parameters using POST or vice versa..
-
If you change this in your javascript???
var url = "calendar_start.php"; var vars = "showmonth="+showmonth+"&showyear="+showyear; var final_url = url+"?"+vars; alert(final_url); //this line should a message with the url, post it please. hr.open("GET",final,true);
That should works.
PS: You pass your url with get params so in you php file you should receive them like
$showmonth = $_GET['showmonth']; //I change POST by GET $showyear = $_GET['showyear']; //DONT forget to chenge this in your php file
Saludos 😉
Originally posted 2013-11-09 23:20:05.