can someone tell me if I need AJAX to do what I need here? I have this function which creates a calendar. I'm going to insert some 'forward' and 'backward' arrows keys so the user can scroll through the months.
So on clicking the forward button; I want to get the next month - and display that month instead of the one present. This means reloading the function, right? But with a different variable for month. Also, I can't have the PHP display itself once more without using JavaScript of some sort, right?
I know I can make the same calendar with JavaScript but I want to see how it works using PHP. I've also never used AJAX functionality before, I'm not sure how I'd change the variable of a script in this way really..
function create_Calendar($month,$year){
$i = "";
$days_array = array("Mon","Tue","Wed","Thur","Fri","Sat","Sun");
$days_in_month = cal_days_in_month(CAL_GREGORIAN,$month,$year);
$jd = cal_to_jd(CAL_GREGORIAN,$month,date("d"),$year);
$first_day = jddayofweek($jd,0);
$month_name = jdmonthname($jd,3) . " " . date("Y");
echo("<div id='calendar'>");
echo("<div id='month_name'>" . $month_name . "</div>");
for($i=0;$i<7;$i++){
echo("<div class='day_titles'>" . $days_array[$i] . "</div>");
}
for($i=0;$i<$first_day;$i++){
echo("<div class='blank_days'></div>");
}
for($i=1;$i<=$days_in_month;$i++){
echo("<div class='day'>" . $i . "</div>");
}
echo("<div class='day_tooltip'></div>");
echo("</div");
};
I am calling the function with:
create_Calendar(date("m"),2013);
Any help is appreciated.
Two simple ajax calls will do the job for you. Make calls with $.ajax and send the month name as argument, when user clicks next or previous buttons. Pre-loading all the months can also do your job, but your script will become more resource intensive in that case.
UPDATE: For example, you can call like this:
<script type="text/javascript">
$(document).ready(function(){
$('#next').click(function(){
var month = 'July';
var year = '2011'; // or get them from the user's choice. I am hardcoding them here
var url = "http://domain/path/to/your/calander_function.php";
$.ajax({'url' : url ,
'type' : 'post',
'data' : {'month' : month , 'year' : year} ,
'success' : function(data){
// do something with your data
}
});
$('#previous').click(function(){
var month = 'June';
var year = '2013'; // or get them from the user's choice. I am hardcoding them here
var url = "http://domain/path/to/your/calander_function.php";
$.ajax({'url' : url ,
'type' : 'post',
'data' : {'month' : month , 'year' : year} ,
'success' : function(data){
// do something with your data
}
});
});
</script>
calander_function.php would contain the function which would echo the result, which you will get in the success ( in data). Hope it helps :-)
PHP is a server-side language. You cannot perform PHP related tasks (without using AJAX) without refreshing the page. Javascript, JQuery, AJAX are all client side. You can perform operations with these without refreshing the page as they do not need to go to the server to perform actions.
So if you do not want to refresh the page, use a client side scripts like javascript or jQuery. You can find good calendars if you google.
The data from webpage goes to server and then server generate the output, it means that PHP will not be running on the server if you keep open the page. So when you request a URL the complete PHP application executes from very start of includes and until the page requested ends. and this will keep on going for each request.
Here is the diagram to better understand the flow.
So each time you have to execute your page. It is via AJAX or page refresh is upto you.