我有一个带有2个选项的下拉列表(LOCAL和IMPORT)。导入选项正常工作,我可以获取值并回显 $ refnumb (IMCK2016-0000001)。但是当我选择LOCAL时,我得到 $ refnumb IMCK2016-0000001 的相同值,而不是 LOCCK2016-0000001 。请帮我查一下我的代码有什么问题。感谢。
<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);
?>
参考编号:
<input type='text' name='ref_no1' id='ref_no1' value="<?php echo $refnumb ?>" readonly />
这里是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;
}
答案 0 :(得分:0)
您的变量$keyname
正在for循环中设置。在打印$refnumb
时,您在循环外引用$keyname
。这意味着您将获得$shipmenttype
中的最后一个元素是&#34; IM&#34;)。
要解决此问题,您需要跟踪所选值。
<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);
?>