Set value of input element using javascript and get the value using php

Here I have a form and input element.

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 javascript is:

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

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

How I can set the value of input element using javascript and acess ths from another page

since the form submits to cart.php, I would expect that result for cart2.php.

There are issues with the first section of code that you show - while() loops that open but do not close, typos (“input tpe=” for example) , using the same id for multiple fields within the loop (or maybe within the loop, I can’t see where it closes), that will cause problems. Especially as you are then using JS to ‘getElementById’ later on. Makes it difficult to figure out exactly what’s wrong when there are bits missing.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.