0

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" />&nbsp;&nbsp;&nbsp; <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

4
Contributors
5
Replies
6
Views
4 Years
Discussion Span
Last Post by adam.adamski.96155
0

see line 64 to 68, i m updating it in our code

  while ( $row = mysql_fetch_array( $result ) ) 
  {
      if($row['bankID']==$_POST['bankID'])
         $selectbank=" selected ";
       else
         $selectbank="";
      ?>
      <option value="<?php echo $row['bankID']; ?>"  <?php echo $selectbank;?> ><?php echo $row['bank']; ?></option>
       <?php
      }
0

In your loop, test each value against the one that was submitted. If they are a match, mark that option as selected:

<option value="<?php echo $row['bankID']; ?>" <?php echo ($row['bankID'] == $_POST['bankID'] ? 'SELECTED="selected"' : '');?> ><?php echo $row['bank']; ?></option>
  • The ? is the ternary operator which works like a shorthand for if...else.
0

thanks guys for your help. the form is now working as i want it to

0

it's working but it's giving a warning of undefined index on the line where we test if $row['bankID'] == $_POST['bankID']

0

if(isset($_POST['id']) && ($row['bankID'] == $_POST['id']))
or,
if(isset($row['bankID']) && ($row['bankID'] == $_POST['id']))
depending which key is not set.

It's good that you have all errors showing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.