猿问

如何使用按钮回显php中的函数?

我正在制作一个事件日历。我想使用按钮在 php 中回显一个函数。


我的实际代码:


日历.php


<?php include_once('functions.php'); ?> 

<?php echo getCalender(); ?>

函数.php


<?php


if(isset($_POST['func']) && !empty($_POST['func'])){

    switch($_POST['func']){

        case 'getCalender':

            getCalender($_POST['year'],$_POST['month']);

            break;

        case 'getEvents':

            getEvents($_POST['date']);

            break;

        default:

            break;

    }

}

//Get calendar full HTML

function getCalender($year = '',$month = '')

{

    $dateYear = ($year != '')?$year:date("Y");

    $dateMonth = ($month != '')?$month:date("m");

    $date = $dateYear.'-'.$dateMonth.'-01';

    $currentMonthFirstDay = date("N",strtotime($date));

    $totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);

    $totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);

    $boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;

?>

    <div id="calender_section">

    //

}

 <script type="text/javascript">

    function getCalendar(target_div,year,month){

        $.ajax({

            type:'POST',

            url:'functions.php',

            data:'func=getCalender&year='+year+'&month='+month,

            success:function(html){

                $('#'+target_div).html(html);

            }

        });

    }


function getEvents(date){

        $.ajax({

            type:'POST',

            url:'functions.php',

            data:'func=getEvents&date='+date,

            success:function(html){

                $('#event_list').html(html);

                $('#event_add').slideUp('slow');

                $('#event_list').slideDown('slow');

            }

        });

    }

现在我想使用按钮来调用 getcalendar 函数。我试过这个:


日历.php


 <form class="" action="" method="post">

               <input type="button" value="Dhaka" onclick="<?php echo getCalender(); ?>"/>

              </form>

现在这不是等待按钮点击。当我刷新页面时,即使我没有单击按钮,它也会显示日历。我该如何解决这个问题?


慕村225694
浏览 185回答 2
2回答

米脂

这是functions.php的代码。你提到了 getCalender 而不是 getCalendar<?php include_once('functions.php'); ?><form class="" action="" method="post"><input type="button" value="Dhaka" onclick="getCalendar()"/></form>另外,关闭functions.php 文件中getEvents 函数之后的脚本标记。

至尊宝的传说

您可以使用print_f(Object);然后试试你的代码,而不是 echo;
随时随地看视频慕课网APP
我要回答