1

我是 PHP 和 javascript 编程的新手。我有一个使用 PHP 回显的链接。

<?php .....echo "<td><font class='calendar'><a onclick='showevent($currentday)' href='#'>". $currentday. "</a></font></td>"; ....?>

我需要从上面的链接调用函数 showevent()。

<script>

function showevent($day){
     $thisday = $day;
     document.showevent.submit();
}

</script>

该函数将提交如下表格:

<form action="./event.php" name="showevent" method="post">
    <input type="hidden" name="page" value="showevent" /> 
    <input type="hidden" name="month" value="<?php echo $cMonth;?>" /> 
    <input type="hidden" name="year" value="<?php echo $cYear; ?>" />
    <input type="hidden" name="day" value="<?php echo $thisday; ?>" />
</form>

javascript函数不起作用。任何人都可以帮助建议和指导我吗?

完整的html代码:

<!DOCTYPE html>

<html>

<head>
<link rel="stylesheet" type="text/css" href="./css/mystyle.css">
<script>
function showevent($day){
    $thisday = $day;
    document.showevent.submit();
}</script>
</head>

<body>
    <p class="menutitle">Event Calendar</p>
    <?php



    $monthNames = Array("January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December");

    if (!isset($_REQUEST["month"])){
        $_REQUEST["month"] = date("n");
    }
    if (!isset($_REQUEST["year"])) {
        $_REQUEST["year"] = date("Y");
    }

    $cMonth = $_REQUEST["month"];
    $cYear = $_REQUEST["year"];

    $prev_year = $cYear;
    $next_year = $cYear;
    $prev_month = $cMonth-1;
    $next_month = $cMonth+1;

    if ($prev_month == 0 ) {
        $prev_month = 12;
        $prev_year = $cYear - 1;
    }
    if ($next_month == 13 ) {
        $next_month = 1;
        $next_year = $cYear + 1;
    }
    ?>

    <table class="calendar">
        <tr align="center">
            <td class="calendarTop">
                <table class="calendarNav">
                    <tr>
                        <td class="calendarOption" onmouseover="this.className='calendarOptionHover'" onmouseout="this.className='calendarOption'" align="center"><a href="#" onclick="document.forms.prev.submit()">Previous</a>
                        </td>
                        <td width="60%"><p class="subtitle"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></p></td>
                        <td class="calendarOption" onmouseover="this.className='calendarOptionHover'" onmouseout="this.className='calendarOption'" align="center"><a href="#" onclick="document.forms.next.submit()">Next</a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td >
                <table class="calendarTop">
                    <tr>
                        <td class="calendarTitle" ><font>S</font>
                        </td>
                        <td class="calendarTitle" ><font>M</font>
                        </td>
                        <td class="calendarTitle" ><font>T</font>
                        </td>
                        <td class="calendarTitle" ><font>W</font>
                        </td>
                        <td class="calendarTitle" ><font>T</font>
                        </td>
                        <td class="calendarTitle" ><font>F</font>
                        </td>
                        <td class="calendarTitle" ><font>S</font>
                        </td>
                    </tr>

                    <?php 

                    require "./dbconnect.php";

                    $result = mysql_query(" SELECT * FROM event WHERE month = $cMonth AND year=$cYear ORDER BY date"); //---->resource

                    $before =0;
                    $after =99;
                    $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
                    $maxday = date("t",$timestamp);
                    // $cMonth;  ---> month name
                    $thismonth = getdate ($timestamp);
                    $startday = $thismonth['wday'];
                    $eventday = Array(31);
                    for ($k =0; $k<32; $k++){
                        $eventday[$k]=false;
                    }
                    while ($row = mysql_fetch_array($result)){
                    $eventday[$row['date']]=true;
                    }

                    for ($i=0; $i<($maxday+$startday); $i++) {
                    if(($i % 7) == 0 ) {
                        echo "<tr>";
                    }
                    if($i < $startday) {
                        echo "<td></td>";
                    }else{
                        $currentday = ($i - $startday + 1);
                        if($eventday[$currentday] == true){

                            echo "<td class='eventtrue'><font class='calendar'><a onclick='javascript: showevent($currentday);' href='#'>". $currentday. "</a></font></td>";
                        }else{
                            echo "<td class='eventfalse'><font class='calendar'>". ($i - $startday + 1). "</font></td>";
}
                    }//--2 brackets

                    if(($i % 7) == 6 ) {
                        echo "</tr>";

                }
}

?>

                </table>
        <tr>
            <td><form action="./event.php" name="prev" method="post">
                    <input type="hidden" name="page" value="calendar" /> <input
                        type="hidden" name="month" value="<?php echo $prev_month;?>" /> <input
                        type="hidden" name="year" value="<?php echo $prev_year; ?>" />
                </form>
                <form action="./event.php" name="next" method="post">
                    <input type="hidden" name="page" value="calendar" /> <input
                        type="hidden" name="month" value="<?php echo $next_month;?>" /> <input
                        type="hidden" name="year" value="<?php echo $next_year; ?>" />
                </form>
                <form action="./event.php" name="showevent" method="post">
                    <input type="hidden" name="page" value="showevent" /> 
                    <input type="hidden" name="month" value="<?php echo $cMonth;?>" /> 
                    <input type="hidden" name="year" value="<?php echo $cYear; ?>" />
                    <input type="hidden" name="day" value="<?php echo $thisday; ?>" />
                </form></td>
        </tr>
        <tr>
        <td>
        </td>
        </tr>
    </table>
</body>

</html>
4

2 回答 2

1
<script>
function showevent(day)
{
    alert(day); // test if it triggers
    document.showevent.day.value = day;
    document.showevent.submit();
    return false;
}
</script>

还:

<a onclick='javascript: showevent($currentday);'

应该:

<a onclick='return showevent($currentday);'

于 2013-01-02T08:26:58.420 回答
-2

请在脚本标签中添加类型并尝试

<script type="text/javascript">

function showevent($day){
     $thisday = $day;
     document.showevent.submit();
}

</script>

希望这可以帮助你解决你的问题......

于 2013-01-02T08:42:54.977 回答