dt3674 2013-02-22 10:40
浏览 56
已采纳

在PHP脚本中更改变量并再次运行函数,无需重新加载页面

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.

  • 写回答

3条回答 默认 最新

  • doude2635 2013-02-22 11:07
    关注

    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 :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog