我在使用onclick函数获取两个输入文本框的值时遇到问题。
<?php foreach ($showItems as $items) { ?>
<tr>
<td><?php if(!empty($items['ItemCode'])) echo $items['ItemCode'], ' '; ?><input type="hidden" name="pid" id="pid" value="<?php echo $items['ItemCode']; ?>" /></td>
<td><img src="" /></td>
<td><?php if(!empty($items['Name'])) echo $items['Name'], ' '; ?></td>
<td><?php if(!empty($items['Price'])) echo $items['Price'], ' '; ?></td>
<td><?php if(!empty($items['Description'])) echo $items['Description'], ' '; ?></td>
<td><input type="text" name="qty" id="qty" maxlength="3" size="2" onblur='if(this.value==""){this.value=1;}' /></td>
<td ><input type="submit" value="Add to Registry" onClick="addRegistry(<?php echo $items['ItemCode']; ?>, document.getElementById('qty').value;)"></td>
</tr>
<?php } ?>
javascript脚本:
function addRegistry(pid, qty){
alert(qty);
document.form.pid.value=pid;
document.form.command.value='add';
document.form.submit();
}
php脚本:
if($_REQUEST['command']=='add' && $_REQUEST['pid']>0){
$qty = $_POST['qty'];
//echo $qty;
$customerid = $_SESSION['id'];
$pid=$_REQUEST['pid'];
// echo $pid;
$admin->addToRegistry($pid, $customerid, $qty);
header('location:registryCart.php');
}
如何获取这两个值,以便我可以将其插入我的数据库,我能够获得itemcode,但我无法获得数量的值。
答案 0 :(得分:1)
使用$counter
变量,该变量将增加并区分每个输入id
<?php
$counter=1;
foreach ($showItems as $items) { ?>
<tr>
<td><?php if(!empty($items['ItemCode'])) echo $items['ItemCode'], ' '; ?><input type="hidden" name="pid" id="pid" value="<?php echo $items['ItemCode']; ?>" /></td>
<td><img src="" /></td>
<td><?php if(!empty($items['Name'])) echo $items['Name'], ' '; ?></td>
<td><?php if(!empty($items['Price'])) echo $items['Price'], ' '; ?></td>
<td><?php if(!empty($items['Description'])) echo $items['Description'], ' '; ?></td>
<td><input type="text" name="qty" id="qty-<?php echo $counter; ?>" maxlength="3" size="2" onblur='if(this.value==""){this.value=1;}' /></td>
<td ><input type="submit" value="Add to Registry" onClick="addRegistry(<?php echo $items['ItemCode']; ?>, document.getElementById('qty-<?php echo $counter; ?>').value;)"></td>
</tr>
<?php
$counter++;
} ?>