获取选择选项的值

I have a dropdown list with 2 option (LOCAL and IMPORT). The option for Import is working and I can get the value and echo the $refnumb (IMCK2016-0000001). But when I select LOCAL I get the same value of $refnumb IMCK2016-0000001 instead of LOCCK2016-0000001. Please help me find what's wrong with my code. Thanks.

<table id='tblselect' class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type: </th>       
<!--<tr><td><input type='submit' name='lookup_master' value='COPY' /></td></tr>-->
<?php $shipmenttype = array("0"=>"Please select...", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>

<td><b><select name='shiptype' id='shiptype'>
    <?php foreach($shipmenttype as $keyname=>$ship_type):?>

        <?php 
            $selected ="";
                    $shiptype = $_POST['shiptype'];
                    if($keyname == $shiptype): $selected =" selected"; endif;
        ?>                    
            <option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
            <?php endforeach;?>
</select></b></td></tr>
</table>

<br><br>

<?php


$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($keyname);

if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}
$refnumb = $keyname.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT);
?>

Reference Number:

<input type='text' name='ref_no1' id='ref_no1' value="<?php echo $refnumb ?>" readonly />

Here's the function for dbNextID.

function dbNextID($key) {
$sql1 = "insert into key_master (keyname) values (:keyname)";
$sql2= "update key_master set id = id + 1 where keyname = :keyname";
$sql3 = "select id from key_master where keyname = :keyname";

$conn = dbConnect(); 
$stmt1 = $conn->prepare($sql1);
$stmt2 = $conn->prepare($sql2);
$stmt3 = $conn->prepare($sql3);

$conn->beginTransaction();
$stmt1->execute(array(':keyname' => $key));
$stmt2->execute(array(':keyname' => $key));
$stmt3->execute(array(':keyname' => $key)); 
$value = $stmt3->fetchColumn(0);
$conn->commit();
$conn=null;

return $value;
}

Your variable $keyname is getting set inside your for loop. When you are printing out $refnumb, you are referencing $keyname outside the loop. This means that you will get the last element in $shipmenttype is "IM").

To fix this, you will need to track the selected value.

<table id='tblselect' class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type: </th>       
<!--<tr><td><input type='submit' name='lookup_master' value='COPY' /></td></tr>-->
<?php $selectedValue = 0; $shipmenttype = array("0"=>"Please select...", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>

<td><b><select name='shiptype' id='shiptype'>
    <?php foreach($shipmenttype as $keyname=>$ship_type):?>

        <?php 
            $selected ="";
                    $shiptype = $_POST['shiptype'];
                    if($keyname == $shiptype) $selected =" selected";$selectedValue = $keyname; endif;
        ?>                    
            <option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
            <?php endforeach;?>
</select></b></td></tr>
</table>

<br><br>

<?php


$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($selectedValue);

if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}
$refnumb = $selectedValue.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT);
?>