i have a form with a text box and a drop-down list. the form submits to itself. when the user submits the form for processing the form must be able to retain the selected value from the drop list if there is an error. here is the code for the form
<?php
session_start(); // start a session
require_once 'includes/db_connect.php';
$page_title = 'Add New Bank Branch';
require_once 'includes/header.php';
require_once 'includes/db_connect.php';
require_once 'includes/functions.php';
if ( isset( $_POST['submitted'] ) ) {
$errors = array();
if ( $_POST['bankID'] == "" ) {
$errors[] = 'Please select the bank to which the branch belongs.';
} else {
$bankId = (int)$_POST['bankID'];
}
if ( $_POST['txtBranchName'] == "" ) {
$errors[] = 'Please enter the new branch\'s name.';
} elseif ( strlen( $_POST['txtBranchName'] ) > 30 ) {
$errors[] = 'The new branch\'s name must not have more than thirty(30) characters.';
} else {
$branchname = ucfirst( escape_value( $_POST['txtBranchName'] ) );
}
if ( empty( $errors ) ) {
$userid = $_SESSION['userid'];
$sql = "INSERT INTO tblbankbranches ( bankID, branch, userID, dateCreated, changedUserID ) VALUES ";
$sql .= " ( $bankId, '{$branchname}', $userid, now(), $userid )";
$result = mysql_query( $sql );
if ( mysql_affected_rows() == 0 ) {
$errors[] = 'Could not add the new branch. Try again later.';
} else {
redirect_to( 'bankbranches.php' );
}
}
}
?>
<div class="title"><?php echo $page_title; ?></div>
<?php checkUser(); ?>
<?php
if ( isset( $errors ) && is_array( $errors ) ) {
// there are form submission errors, which must be displayed
echo '<ul>';
foreach ( $errors as $warning ) {
echo "<li class='error'>$warning</li>";
}
echo '</ul>';
}
?>
<form action="" method="post" >
<table cellpadding = "5" cellspacing = "5" >
<tr>
<td align="left">Bank:</td>
<td><select name="bankID">
<option value="">Select the bank</option>
<option value="">----------------------</option>
<?php
$sql = "SELECT bankID, bank FROM tblbanks ORDER BY bank ASC";
$result = mysql_query( $sql );
while ( $row = mysql_fetch_array( $result ) ) {
?>
<option value="<?php echo $row['bankID']; ?>"><?php echo $row['bank']; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Branch Name:</td>
<td><input type="text" name="txtBranchName" id="txtBranchName" size="30" value="<?php if ( isset( $_POST['txtBranchName'] ) ) echo $_POST['txtBranchName']; ?>" /></td>
</tr>
<tr>
<td align="right" colspan="2"><input type="submit" value="Add Branch" /><input type="hidden" name="submitted" /> <input type="button" value="Cancel" onclick="window.location.href='bankbranches.php'" /></td>
</tr>
</table>
</form>
<?php
require_once 'includes/footer.php';
?>
what changes do i have to make to my code