简体   繁体   English

使用javascript设置input元素的值并使用php获取值

[英]Set value of input element using javascript and get the value using php

Here I have a form and input element: 这里我有一个表单和输入元素:

cart.php 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>

and the javascript is: 而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> 

when i am accessign the value of s1 element in cart2.php it gives nothing. 当我在cart2.php中访问s1元素的值时,它什么也没有给出。 on next page s1 has no value,while i am expecting the value that is updated using javascript 在下一页上,s1没有值,而我期待使用javascript更新的值

$prod_id = $_POST['s1'];
echo "product id of clickable product is:".$prod_id."<br>";

this line: 这一行:

<input type="text" name="s1" id="s1" value=""/>

is inside a loop. 在一个循环中。 can you make sure that the loop only has one row returning? 你能确保循环只返回一行吗? because if not, then you're creating multiple elements with same id which javascript can not catch properly. 因为如果没有,那么你正在创建多个具有相同id元素,javascript无法正确捕获。 id is supposed to be unique. id应该是唯一的。 in the case of multiple same ids it'll catch the first match and stop. 在多个相同ID的情况下,它将捕获第一个匹配并停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM