获取选择选项的值不起作用

时间:2016-01-11 08:41:17

标签: php sql

我有一个带有2个选项的下拉列表(LOCAL和IMPORT)。如果我选择LOCAL,我应该在IMPORT得到值LOC,我应该得到IM。但无论是我选择的两个,我都得到IM值。请帮忙,如何获取相应选择选项的值。谢谢。

<?php  $shipmenttype = array("0"=>"Please select", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>
<td><b><select name='shiptype' id='shiptype' onchange=''>
    <?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>


<?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); 

?>

正在运行的号码(000000):

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;
}

HTML表:

<!--- Local Shipment --->

<table id="t1" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship1' id='ship1' value="" readonly /></b></td></tr>

<tr><th align='right'>Reference Number: </th>
<td><input type='text' name='ref_no1' id='ref_no1' value="<?php echo $refnumb ?>" readonly /></td></tr>

<tr><th align='right'>Supplier: </th>
<td><select name='supplier1'>
    <?php foreach($data1 as $row1): ?>
    <option value="<?php echo $row1['SupplierName'] ?>"><?php echo $row1['SupplierName'] ?></option>
    <?php endforeach; ?>
    </select></td></tr>

<tr><th align='right'>Description: </th>
<td><input type='text' name='description1' size='70' /></td></tr>

<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no1' /></td></tr> 

<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number1' /></td></tr>

<tr><th align='right'>Receive Qty: </th>
<td><input type='text' name='r_qty1' /></td></tr>

<tr><th align='right'>No. of Boxes/Packs: </th>
<td><input type='text' name='no_boxes1' /></td></tr>

<tr><th align='right'></th>
<td></td></tr>

<tr><th align='right'> </th>
<td align='left'><input type='submit' name='local_add' value='SAVE' style='font-size:2em;' /> &nbsp;&nbsp;&nbsp;&nbsp; <input type='submit' name='local_cancel' value='CANCEL' style='font-size:2em;' /></td></tr>
</table>


<!--- Import Shipment --->

<table id="t2" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship2' id='ship2' value="" readonly /></b></td></tr>

<tr><th align='right'>Reference Number: </th>
<td><b><input type='text' name='ref_no2' id='ref_no2' value="<?php echo $refnumb ?>" readonly /></b></td></tr>

<tr><th align='right'>Supplier: </th>
<td><select name='supplier2'>
    <?php foreach($data2 as $row2): ?>
    <option value="<?php echo $row2['SupplierName'] ?>"><?php echo $row2['SupplierName'] ?>
    <?php endforeach; ?>
    </select></td></tr>

<tr><th align='right'>Description: </th>
<td><input type='text' name='description2' size='70' /></td></tr>

<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no2' /></td></tr>

<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number2' /></td></tr>

<tr><th align='right'>AWB(Airway Bill) No: </th>
<td><input type='text' name='awb_no2' /></td></tr>

    <tr><th align='right'>AWB No of Boxes/Packs: </th>
<td><input type='text' name='awb_no_boxes2' /></td></tr>

<tr><th align='right'> </th>
<td></td></tr>

<tr><th align='right'> </th>
<td align='left'><input type='submit' name='import_add' value='SAVE' style='font-size:2em;' /> &nbsp;&nbsp;&nbsp;&nbsp; <input type='submit' name='import_cancel' value='CANCEL' style='font-size:2em;' /></td></tr>
</table>

Jquery的:

<script>
$('#shiptype').change(function () {
$('#t1,#t2').hide();
    var selectedValue = $(this).val();
var strloc = "LOCAL";
var strimp = "IMPORT";

    if (selectedValue == "LOC") {
        $('#t1').show();
    $('#ship1').val(strloc);
    //$('#strkey').val(selectedValue);

    } else if (selectedValue == "IM") {
        $('#t2').show();
    $('#ship2').val(strimp);
    //$('#strkey').val(selectedValue);
    }           
 });

$('#t1,#t2').hide();

</script>

1 个答案:

答案 0 :(得分:0)

您可以在代码中尝试此操作:

$shiptype = $_POST['shiptype'];
$selected = ($shiptype == $keyname ? 'selected=""' : '');

您的完整代码(已修改):

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

<?php 
$shiptype = $_POST['shiptype'];
$selected = ($shiptype == $keyname ? 'selected=""' : '');
?>                    
<option value="<?php echo $keyname;?>" <?php echo $selected;?>>

<?php echo $ship_type;?>
</option>
<?php endforeach;?>

</select>

使用现有代码解决方案:

$selected = "";
if($keyname == $shiptype): $selected ='selected=""'; 
endif;       

我改变了什么?

只需替换:

$selected ="selected";

。通过

$selected ='selected=""';