这里我有一个表单和输入元素:
cart.php
<form id="cartform" action="cart.php?action=update&pd=<?php echo $row['product_id'] ?>" name="form1" method="post">
<?php
$query = "select * from cart where customer_id='$user' ";
$result = mysqli_query($con,$query);$payableamount = 0;$totalsavings = 0;
while($row = mysqli_fetch_array($result))
{
$productid = $row['product_id'];
$query2 = "select * from product where product_id='$productid'";
$result2 = mysqli_query($con,$query2);
while($row2=mysqli_fetch_array($result2))
{
?>
<input tpe="text" name="quantxt[]" id="quantxt" value="<?php echo $qty = $row['quantity']; ?>" onkeyup="showsubmit(<?php echo $row['cart_id'] ?>,<?php echo $row['product_id'] ?>)">
<input type="text" name="s1" id="s1" value=""/>
<input style="visibility:hidden;width:80px;border-radius:10px;background-color:green;border:none;padding:5px;color:white;"
type="submit"
name="sub_<?php echo $row['cart_id'] ?>"
id="sub_<?php echo $row['cart_id'] ?>"
value="UPDATE">
</table>
</form>
和javascript是:
<script>
function showsubmit(id,prodid)
{
alert(id);
alert(prodid);
document.getElementById("sub_"+id).style.visibility ="visible";
document.getElementById("s1").value = prodid;
f = document.getElementById("s1").value;
alert("product id is:"+f);
}
</script>
当我在cart2.php中访问s1元素的值时,它什么也没有给出。 在下一页上,s1没有值,而我期待使用javascript更新的值
$prod_id = $_POST['s1'];
echo "product id of clickable product is:".$prod_id."<br>";
答案 0 :(得分:0)
这一行:
<input type="text" name="s1" id="s1" value=""/>
在循环中。你能确保循环只返回一行吗?因为如果没有,那么你正在创建多个具有相同id
的元素,javascript无法正确捕获。 id
应该是唯一的。在多个相同ID的情况下,它将捕获第一个匹配并停止。