lipe009 Posted August 5, 2013 Share Posted August 5, 2013 So, i made a seat booking system where there are checkboxes in every seat and the user select this boxes and the seats are considered reserved. But now i want make the user be able to give each marked seat a name. How can i do that? seats.php <html><head><title>Tickets</title><style>* {font-size: 11px;font-family: arial;}</style><script>function reserveSeats() {var selectedList = getSelectedList('Reserve Seats');if (selectedList) {if (confirm('Do you want to reserveselected 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 listvar 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 errorif (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()'/> <input type='button' value='Confirm Reservation' onclick='confirmSeats()'/> <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", "tickets", "tickets") or die("Could not connect to MySQL server");@ mysql_select_db("tickets") 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 == $_SERVER['PHP_AUTH_USER'])) {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> </td>";}}echo "</tr></table></td>";echo "</tr>";echo "</table>";/* Close connection to database server. */mysql_close();?></td></tr><tr><td> </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><td bgcolor='FFCCFF'>Reserved user2</td><td bgcolor='CC66FF'>Confirmed user2</td></tr></table></td></tr><tr><td> </td></tr><tr><td width="100%" align="center"><a href="seatplan.jpg" target="new">View Layout</a> </td></tr></table></form></body></html> bookseats.php <html><head><title>Book Seats</title><style>* {font-size: 14px;font-family: arial;}</style></head><body><center><br/><br/><br/><?phpif (isset($_POST['seats'])){$user = $_SERVER['PHP_AUTH_USER'];$newStatusCode = $_POST['newStatusCode'];$oldStatusCode = $_POST['oldStatusCode'];// open database connection$linkID = @ mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");@ mysql_select_db("tickets") or die("Could not select database");// prepare select statement$selectQuery = "SELECT rowId, columnId from seats where (";$count = 0;foreach($_POST['seats'] AS $seat) {if ($count > 0) {$selectQuery .= " || ";}$selectQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";$selectQuery .= " and columnId = " . substr($seat, 1) . " ) ";$count++;}$selectQuery .= " ) and status = $oldStatusCode";if ($oldStatusCode == 1) {$selectQuery .= " and updatedby = '$user'";}//echo $selectQuery;// execute select statement$result = mysql_query($selectQuery);//echo $result;$selectedSeats = mysql_num_rows($result);//echo "<br/>" . $selectedSeats;if ($selectedSeats != $count) {$problem = "<h3>There was a problem executing your request. No seat/s were updated.</h3>";$problem .= "Possible problems are:";$problem .= "<ul>";$problem .= "<li>Another process was able to book the same seat while you were still browsing.</li>";$problem .= "<li>You were trying to Confirm an unreserved Seat.</li>";$problem .= "<li>You were trying to Cancel an unreserved Seat.</li>";$problem .= "<li>You were trying to Reserve a reserved Seat.</li>";$problem .= "<li>There was a problem connecting to the database.</li>";$problem .= "</ul>";$problem .= "<a href='seats.php'>View Seat Plan</a>";die ($problem);}// prepare update statement$newStatusCode = $_POST['newStatusCode'];$oldStatusCode = $_POST['oldStatusCode'];$updateQuery = "UPDATE seats set status=$newStatusCode, updatedby='$user' where ( ";$count = 0;foreach($_POST['seats'] AS $seat) {if ($count > 0) {$updateQuery .= " || ";}$updateQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";$updateQuery .= " and columnId = " . substr($seat, 1) . " ) ";$count++;}$updateQuery .= " ) and status = $oldStatusCode";if ($oldStatusCode == 1) {$updateQuery .= " and updatedby = '$user'";}// perform update$result = mysql_query($updateQuery);$updatedSeats = mysql_affected_rows();if ($result && $updatedSeats == $count) {//$mysql->commit();include ('reservapt2.php');} else {//$mysql->rollback();echo "<h3>There was a problem executing your request. No seat/s were updated.</h3>";echo "Possible problems are:";echo "<ul>";echo "<li>Another process was able to book the same seat while you were still browsing.</li>";echo "<li>You were trying to Confirm an unreserved Seat.</li>";echo "<li>You were trying to Cancel an unreserved Seat.</li>";echo "<li>You were trying to Reserve a reserved Seat.</li>";echo "<li>There was a problem connecting to the database.</li>";echo "</ul>";}echo "<a href='seats.php'>View Seat Plan</a>";// Enable the autocommit feature//$mysqldb->autocommit(TRUE);// Recuperate the query resources//$result->free();mysql_close();}?></center></body></html> ----------------------------- I tried the following: When the user submit the checkboxes, this is included: name.php <?php//$mysql->commit();session_start();$_SESSION['rowId'] = $_POST['rowId'];$_SESSION['columnId'] = $_POST['columnId'];echo "<h3>";echo "Please enter the name for each seat:<br><p> </p>";echo "";foreach($_POST['seats'] AS $seat) {$rowId = substr($seat, 0, 1);$columnId = substr($seat, 1);echo $rowId . $columnId . '</br><form method="post" name="input" action="pt2.php" ><input name="name" type="text"/></br>';}?><input type="submit" name="Submit" value="insert" /></form> pt2.php: <?php// Connect to MySQLmysql_connect("localhost", "root", "root") or die("Connection Failed");mysql_select_db("tickets")or die("Connection Failed");$name = $_POST['name'];session_start();$rowId = $_SESSION['rowId'];$columnId = $_SESSION['columnId'];$result = mysql_query("UPDATE seats SET updatedby='".$name."' WHERE rowId='".$rowId."' AND columnId='".$columnId."'") or die(mysql_error()); ?> If the user selects only one seat, this code works like a charm. But when there's two or more, the $columnId and the $rowId doesn't change as it should, and only the name of the last checked seat is changed. Does anybody knows how i can solve this? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2013 Share Posted August 5, 2013 It looks like you're printing a new form tag for each text field, that's one problem. You should be storing the list of checkbox row/column pairs along with the form data, in a hidden input or something. For the names of the text boxes, use the column and row in the name. When you process the form, get the list of rows and columns and use that to look up the name for each one. Link to comment Share on other sites More sharing options...
lipe009 Posted August 5, 2013 Author Share Posted August 5, 2013 It looks like you're printing a new form tag for each text field, that's one problem. You should be storing the list of checkbox row/column pairs along with the form data, in a hidden input or something. For the names of the text boxes, use the column and row in the name. When you process the form, get the list of rows and columns and use that to look up the name for each one. I'm not sure if I understood what you mean... About the form tag for each text field, I edit the code, so that it doesn't repeat: </br><form method="post" name="input" action="pt2.php" ><?php//$mysql->commit();session_start();$_SESSION['rowId'] = $_POST['rowId'];$_SESSION['columnId'] = $_POST['columnId'];echo "<h3>";echo "Please enter the name for each seat:<br><p> </p>";echo "";foreach($_POST['seats'] AS $seat) {$rowId = substr($seat, 0, 1);$columnId = substr($seat, 1);echo $rowId . $columnId . '<input name="name" type="text"/></br>';}?><input type="submit" name="Submit" value="insert" /></form> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2013 Share Posted August 5, 2013 echo $rowId . $columnId . '<input type="hidden" name="seats[]" value="' . $seat . '"><input name="' . $seat . 'name" type="text"/></br>'; Now you'll have an array in $_POST['seats'] that has each seat, and you can loop through that and get $_POST[$seat . 'name'] to get the name of it. Link to comment Share on other sites More sharing options...
lipe009 Posted August 5, 2013 Author Share Posted August 5, 2013 Now you'll have an array in $_POST['seats'] that has each seat, and you can loop through that and get $_POST[$seat . 'name'] to get the name of it. So let's say i just want to display the name of the seat. Should't this work? <form method="post" name="name" action="pt2.php" ><?php//$mysql->commit();echo "<h3>";echo "Please enter the name for each seat:<br><p> </p>";echo "";foreach($_POST['seats'] AS $seat) {$rowId = substr($seat, 0, 1);$columnId = substr($seat, 1);echo $rowId . $columnId . '<input type="hidden" name="seats[]" value="' . $seat . '"><input name="' . $seat . 'name" type="text"/></br>';}?> <input type="submit" name="submit" value="Submit Form"><br></form> pt2.php <?php// Connect to MySQLmysql_connect("localhost", "root", "root") or die("Connection Failed");mysql_select_db("tickets")or die("Connection Failed");echo $_POST[$seat . 'name'];?> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2013 Share Posted August 5, 2013 You need to define $seat before $_POST[$seat . 'name'] is going to do anything. Like I said, $_POST['seats'] will contain an array of the seats that you can loop through and get the name for each one from $_POST. Link to comment Share on other sites More sharing options...
lipe009 Posted August 5, 2013 Author Share Posted August 5, 2013 (edited) You need to define $seat before $_POST[$seat . 'name'] is going to do anything. Like I said, $_POST['seats'] will contain an array of the seats that you can loop through and get the name for each one from $_POST. Like this? <?php// Connect to MySQLmysql_connect("localhost", "root", "root") or die("Connection Failed");mysql_select_db("iamin")or die("Connection Failed");$seat = $_POST['seats'];echo $_POST[$seat . 'name'];?> Thanks for the answers. Edited August 6, 2013 by lipe009 Link to comment Share on other sites More sharing options...
lipe009 Posted August 6, 2013 Author Share Posted August 6, 2013 (edited) I should use foreach for the loop, right? Like this? foreach ($_POST['seats']; as $seat){?> Edited August 6, 2013 by lipe009 Link to comment Share on other sites More sharing options...
justsomeguy Posted August 6, 2013 Share Posted August 6, 2013 Without the semicolon in the foreach line. Link to comment Share on other sites More sharing options...
lipe009 Posted August 6, 2013 Author Share Posted August 6, 2013 Alright, I'm getting the hang of it. So if I wanna send the name of the seats to the table "seatnames", in the "seatname" row: <?php// Connect to MySQLmysql_connect("localhost", "root", "root") or die("Connection Failed");mysql_select_db("tickets")or die("Connection Failed");$name = $_POST[$seat . 'name']foreach ($_POST['seats'] as $seat){$query = "INSERT INTO seatnames(seatname) VALUES ('$name')";}?> right? Link to comment Share on other sites More sharing options...
lipe009 Posted August 6, 2013 Author Share Posted August 6, 2013 i'm almost there! <?php// Connect to MySQLmysql_connect("localhost", "root", "root") or die("Connection Failed");mysql_select_db("iamin")or die("Connection Failed");$name = $_POST[$seat . 'name'];foreach ($_POST['seats'] as $seat){echo $seat;$query = "INSERT INTO seatnames (seatname) VALUES ('$name')";mysql_query($query) or die(mysql_error()); }?> For some reason it insert a blank space in the table, instead the name! Link to comment Share on other sites More sharing options...
lipe009 Posted August 6, 2013 Author Share Posted August 6, 2013 think for some reason the $_POST[$seat . 'name']; array is empty. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 6, 2013 Share Posted August 6, 2013 Yes, that's empty because you haven't defined $seat before you try to get the value from $_POST. Get the value from $_POST inside the loop, once $seat has a value. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now