JavaScript 无法获取变量复选框


javascript cant get variable in checkbox

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

但是当我尝试在我的网站上实现它时,座位

复选框似乎不起作用,当我按下预订座位按钮时,它总是提醒"请在点击前选择一个座位"(我已经选中了一些框)。

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

这是我的索引.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>

在预订中.php它将包含在索引中.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>

您的问题如下:

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

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

尝试使用这个:

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