javascript无法在复选框中获取变量

时间:2014-12-01 04:53:54

标签: javascript php jquery

我正在尝试实施一个包含座位预订系统的影院网页。我从这个页面http://dayg.wordpress.com/2008/04/17/php-101-a-simple-seat-reservation-system/获得了一些帮助,当我单独测试它时,它的效果非常好。

但是当我尝试将其实施到我的网站时,座位复选框似乎无法正常工作,当我按下预留座位按钮时(我已经检查了一些方框),它始终提醒“请在点击前选择座位”。

我使用index.php并在我的网站上包含每个页面,以便在调用时加载。

这是我的index.php代码:

 <?php 
    ob_start();
    include ('pages/top.php');
    $ui1 = $_GET['ui1'];
    if (!$ui1) { $ui1 = 'movie';}
    $ui2 = $_GET['ui2'];
    if (!$ui2) { $ui2 = 'homepage';}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
    </script>
    <title>4starCine</title>
    <meta name="description" content="4starcinema website" />
    <meta name="keywords" content="4starcinema,4starcine,4star,cine" />
    <meta name="author" content="HTTA group 4" />
    <link rel="stylesheet" href="css/style.css" type="text/css" />
    <link rel="stylesheet" href="css/style2.css" type="text/css" />
</head>
<body>
    <div class="backdrop">


        <div class="wrapper">
            <div class="header">
                <?php include ('pages/head.php')?>
            </div>

            <div class="content">
                <?php 
                if (file_exists('pages/'.$ui1.'/'.$ui2.'.php')) {
                    include ('pages/'.$ui1.'/'.$ui2.'.php');
                    }
                else {
                    include ('pages/notfound.php');
                    }
                ?>
            </div>
            <div class="clear"></div>
            <div class="footer">
                <div class="footer_mini">

                </div>
                <div class="footer_mini">
                </div>
                <div class="footer_mini">
                </div>
                <div class="footer_mini">
                </div>

            </div>

        </div>
    </div>  
</body>
</html>

在booking.php中,它将包含在index.php中:

<?php
    session_start();
    if ($_SESSION['user']!=1){
        header("location:index.php?ui1=member&ui2=login");
    }
?>
<html>
<head>
    <title>Tickets</title>

    <script>

        function reserveSeats() {

            var selectedList = getSelectedList('Reserve Seats');

            if (selectedList) {
                if (confirm('Do you want to reserve selected seat/s ' + selectedList + '?')) {
                    document.forms[0].oldStatusCode.value=0;
                    document.forms[0].newStatusCode.value=1;
                    document.forms[0].action='bookseats.php';
                    document.forms[0].submit();
                } else {
                    clearSelection();
                }
            }
        }


        function cancelSeats() {

            var selectedList = getSelectedList('Cancel Reservation');

            if (selectedList) {
                if (confirm('Do you want to cancel reserved seat/s ' + selectedList + '?')) {
                    document.forms[0].oldStatusCode.value=1;
                    document.forms[0].newStatusCode.value=0;
                    document.forms[0].action='bookseats.php';
                    document.forms[0].submit();
                } else {
                    clearSelection();
                }
            }
        }


        function confirmSeats() {

            var selectedList = getSelectedList('Confirm Reservation');

            if (selectedList) {
                if (confirm('Do you want to confirm reserved seat/s ' + selectedList + '?')) {
                    document.forms[0].oldStatusCode.value=1;
                    document.forms[0].newStatusCode.value=2;
                    document.forms[0].action='bookseats.php';
                    document.forms[0].submit();
                } else {
                    clearSelection();
                }
            }
        }


        function getSelectedList(actionSelected) {

            // get selected list
            var obj = document.forms[0].elements;
            var selectedList = '';
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].checked && obj[i].name == 'seats[]') {
                    selectedList += obj[i].value + ', ';
                }
            }

            // no selection error
            if (selectedList == '') {
                alert('Please select a seat before clicking ' + actionSelected);
                return false;
            } else {
                return selectedList;
            }

        }

        function clearSelection() {
            var obj = document.forms[0].elements;
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].checked) {
                    obj[i].checked = false;
                }
            }
        }


        function refreshView() {
            clearSelection();
            document.forms[0].action='<?php echo $_SERVER['PHP_SELF']; ?>';
            document.forms[0].submit();
        }

    </script>
</head>
<body>
<table>
<tr><td width="100%" align="center">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input type="hidden" name="oldStatusCode" value=""/>
<input type="hidden" name="newStatusCode" value=""/>

<table width='100%' border='0'>
    <tr><td align='center'>
        <input type='button' value='Refresh View' onclick='refreshView();'/>
    </td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
    <tr><td align='center'>
        <input type='button' value='Reserve Seats' onclick='reserveSeats()'/>
        &nbsp;<input type='button' value='Confirm Reservation' onclick='confirmSeats()'/>
        &nbsp;<input type='button' value='Cancel Reservation' onclick='cancelSeats()'/>
    </td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
    <tr><td align='center'>
        <input type='button' value='Clear Selection' onclick='clearSelection()'/></td>
    </tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<?php


$linkID = @ mysql_connect('localhost','huyhd','hdh8623') or die("Could not connect to MySQL server");
@ mysql_select_db("huyhd_4starcine") or die("Could not select database");
/* Create and execute query. */
$query = "SELECT * from seats order by rowId, columnId desc";
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='3' cellspacing='3'>";
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
    if ($prevRowId != $rowId) {
        if ($rowId != 'A') {
            echo "</tr></table></td>";
            echo "\n</tr>";
        }
        $prevRowId = $rowId;
        echo "\n<tr><td align='center'><table border='1' cellpadding='8' cellspacing='8'><tr>";
    } else {
        $tableRow = false;
    }
    if ($status == 0) {
        $seatColor = "lightgreen";
    } else if ($status == 1 && $updatedby == 'user1') {
        $seatColor = "FFCC99";
    } else if ($status == 1 && $updatedby == 'user2') {
        $seatColor = "FFCCFF";
    } else if ($status == 2 && $updatedby == 'user1') {
        $seatColor = "FF9999";
    } else if ($status == 2 && $updatedby == 'user2') {
        $seatColor = "CC66FF";
    } else {
        $seatColor = "red";
    }

    echo "\n<td bgcolor='$seatColor' align='center'>";
    echo "$rowId$columnId";
    if ($status == 0 || ($status == 1 && $updatedby == $_SESSSION['username'])) {
        echo "<input type='checkbox' name='seats[]' value='$rowId$columnId'></checkbox>";
    }
    echo "</td>";
        if (($rowId == 'A' && $columnId == 7) 
            || ($rowId == 'B' && $columnId == 9) 
            || ($rowId == 'C' && $columnId == 9) 
            || ($rowId == 'D' && $columnId == 10) 
            || ($rowId == 'E' && $columnId == 8) 
            || ($rowId == 'F' && $columnId == 5) 
            || ($rowId == 'G' && $columnId == 13) 
            || ($rowId == 'H' && $columnId == 14) 
            || ($rowId == 'I' && $columnId == 14) 
            || ($rowId == 'J' && $columnId == 12) 
            || ($rowId == 'K' && $columnId == 14) 
            || ($rowId == 'L' && $columnId == 13) 
            || ($rowId == 'M' && $columnId == 9)) {
            // This fragment is for adding a blank cell which represent the "center aisle"
            echo "<td>&nbsp;</td>";
        }
}

echo "</tr></table></td>";
echo "</tr>";
echo "</table>";

/* Close connection to database server. */
mysql_close();
?>
</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td width="100%" align="center">
    <table border="1" cellspacing="8" cellpadding="8">
        <tr>
            <td bgcolor='lightgreen'>Available</td>
            <td bgcolor='FFCC99'>Reserved user1</td>
            <td bgcolor='FF9999'>Confirmed user1</td>

        </tr>
    </table>
</td></tr>
<tr><td>&nbsp;</td></tr>

</table>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

您的问题如下所示:

var obj = document.forms[0].elements;

包含复选框的表单不是表单0,因为您有另一种搜索表单。它应该是forms[1]

答案 1 :(得分:0)

尝试使用:

for (var i = 0; i < obj.length; i++) {
if (obj[i].name == 'seats[]') {
  $("input:checkbox[name='type']:checked").each(function()
  {

   selectedList += obj[i].value + ', ';

});

}

}