Jump to content

[SOLVED] Advice on Checkboxes Please?


DanielHardy

Recommended Posts

Hi,

 

Please take a look at my code. I currently have a series of checkboxes designed so that a user can check them, and click the comfirm registratio buton. It is based on a stats feild in the database updating from 0 to  when they do this, and then the corresonding box will turn red. This is possible if I simply update the data in the database. But I am struggling to find a way to get the button to do this for me. Here is the code that controls the checkboxes :

 


<html>
<head>
<title>Tickets</title>
<style>
	* {
		font-size: 14px;
		font-family: arial;
	}
</style>
</head>
<body>
<?php include("login.php"); ?>
<center>
<br/>
<br/>
<br/>
<?php
/*
* Created on Mar 13, 2007
* Author: dayg
*/
if (isset($_POST['seats']))
{
	$user = $_SERVER['PHP_AUTH_USER'];

	$newStatusCode = $_POST['newStatusCode'];
	$oldStatusCode = $_POST['oldStatusCode'];

	// open database connection
	$linkID = @ mysql_connect("localhost", "0604274", "ah3482") or die("Could not connect to MySQL server");
	@ mysql_select_db("db0604274") 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)or die(mysql_error());
	//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();
		echo "<h3>";
		echo "You have successfully updated $updatedSeats seat/s: ";
		echo "[";
		foreach($_POST['seats'] AS $seat) {
			$rowId = substr($seat, 0, 1);
			$columnId = substr($seat, 1);
			echo $rowId . $columnId . ", ";	
		}
		echo "]";
		echo "...</h3>";
	} 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>

 

And here is the one that creates it


<?php include("login.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 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=0;
				document.forms[0].newStatusCode.value=1;
				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()'/>
	 <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
/*
* Created on Mar 17, 2007
* Author: dayg
*/

$linkID = @ mysql_connect("localhost", "0604274", "ah3482") or die("Could not connect to MySQL server");
@ mysql_select_db("db0604274") 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 {
	$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 == 
		|| ($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='FF0000'>Booked</td>

	</tr>
</table>
</td></tr>
<tr><td> </td></tr>
<tr><td width="100%" align="center">
  <a href="file:///C|/DOCUME~1/Dan/LOCALS~1/Temp/Rar$DI01.500/seatplan.jpg" target="new">View Layout</a> 
</td></tr>
</table>
</form>
</body>
</html>

 

Please any ideas on how I can achieve my goal? I know that I need something like a for each wrapped around the checkbox part that tells the database to update the status. But I am struggling to get it working. Any help is greatly apreciated. Thanks in advance.

 

Dan

Link to comment
https://forums.phpfreaks.com/topic/150134-solved-advice-on-checkboxes-please/
Share on other sites

I am confused by your explanation.

 

Are all the the checkboxes related to different fields in the database?

 

I have something similar which may help where you select multiple checkboxes and upadate the database but my example updates each row in the database but there shouldn'e be any reason why you can't update different fields.

The way i did it where it updates multiple row is like this:

 

In my form where the checkboxes are

<input type='checkbox' name='id[]' value='{$row['id']}' />

 

and when i do the query (your query would be different)

foreach($_POST['id'] as $id) {

$sql="DELETE FROM $table WHERE $field1=$id LIMIT 1";

//query to enter form data into database 	    
$result = $db->query($sql);

}

 

I wrote a tutorial on this kind of thing: http://www.phpfreaks.com/tutorial/working-with-checkboxes-and-a-database

 

AdRock - it's pretty inefficient to perform a query for each individual item to be deleted. If you implode the ids you can then use an IN clause (see the tutorial)

Im not sure how to implement your suggestion AdRock. I know i know I obviously need to use Update seats set status=1, or something of the sort. But I am not sure where to implement it. Help would be greatly appreciated as always.

 

 

GingerRobot thanks for the suggestion but I can't seem to get the tutorial working.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.