Pergunta

I need a hand cause I run out of ideas.

I have a ajax script and php script, logic is as followes: 1. PHP script is making a calendar with marked bookings and send it to HTML page, 2. On HTML page user can click on button "Next month" and it sends a ajax request to php script. 3. PHP is doing month++ and sending results.

This work only one time due to reset in php script with every request. month++ it's always only one month forward.

Do you have any ideas how to shuffle through calendar?

PHP:

<?php
require_once ("config.php");

/* Fields counter */
$FieldsNo=1;

/* Getting today's date*/ 
$date = date("d.n.Y");

/* date slicing */
$str = explode(".", $date);
    $day = $str[0];
    $month = $str[1];
    $year = $str[2];
    $gmonth = $month;
    $gyear = $year;

if (isset($_POST['month'])) {

    $month++;
}
require_once ("arrays.php");


/* operation with calendar */
$daysinmonth=cal_days_in_month(CAL_GREGORIAN, $month, $year);
$julianDayCount=cal_to_jd(CAL_GREGORIAN, $month, 1, $year);
$dayOfWeek=jddayofweek($julianDayCount, 0);
$fullies = $daysinmonth;
/* counting amount of empty fields */
switch ($dayOfWeek) {
case 0:
    $empties= 6;
    break;
case 1:
    $empties= 0;
    break;
case 2:
    $empties=1;
    break;
case 3:
    $empties = 2;
    break;
case 4:
    $empties = 3;
    break;
case 5:
    $empties = 4;
    break;
case 6:
    $empties = 5;
    break;
}
/* month translation*/
if ($language == "pl") {
    include("months_pl.php"); 
} else {
    include("months_eng.php"); 
}
/* echoing calendar base */
echo $monthn;
echo $day;
echo "<br>";
if ($language == "pl") {
    include("days_pl.php"); 
} else {
    include("days_eng.php"); 
}

/* first loop - empty fields */
if ($today) {
    include("calendarloopt.php"); 
} else {
    include("calendarloop.php"); 
} 
echo "</tr></tbody></table>";

echo json_encode($loadout);

?>

HTML/AJAX

<!DOCTYPE html>
<html lang="en">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
         <script>
            $(document).ready(function(){
            $("#next").click(function() {
var jqXHR = $.ajax({
    type     : "POST",
    url      : "calendar.php",
    data     : {
            month: 'next'
    },
    success: function(html){

$("#loadplace").html(html);

},
    error:    function(error) {
        //ten fragment wykona się w przypadku BŁĘDU
    }
});
            return jqXHR.responseText;
            });
            });
</script>
        <meta charset="utf-8" />
        <title></title>

    </head>
    <body>
        <input type="button" id="next" value="Next Month"/>


        <div id="loadplace" align="right" width="400" height ="400">

<?php
            include 'calendar.php';
            ?>
        </div>
    </body>
</html>
Foi útil?

Solução

Don't reset the value in the PHP page each time it runs.

Use a session variable instead (and set to a reasonable default if the session variable was not yet set).

Also, your current code will fail when you try to increment December. Have a look at the PHP Datetime::add function

Outras dicas

You should do the month incrementing in the JavaScript and not in the PHP.

An AJAX request should return a value consistent with the parameters it has been passed. The way you want to implement it, it would also depend on an internal variable.

Your actual script also has little flexibility. It can only return the next month. What if you'd like to be able to return the previous month, or a specific month in the future?

In my opinion, you should change your AJAX script either to return the calendar for a specific month, or to return the calendar for the month following (or preceding) a specific month.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top