Hi all expert here,

please refer to attached image, I had a table which mainly stored add-to-cart items, user might able to check what's item was in their cart and do remove or update item's quantity as like common cart we use for online shopping. I was able to loop and called up related records from add-to-cart table and show on front-end, the problem is the submit button (update button), its always post back / pointed to last record, which is J001 with quantity '7' although I try to update K001 or P001 item's quantity, is that any method to solve this kind of problem?

The form I've use is POST method, below echoes is a test whether the submission passing the correct values (unfortunately its keeps return me last row).

if(isset($_POST["btnUpdate"])){
$itm_qty = $_POST["txtQty"];
$hidden_id = $_POST["hidden_id"];

    echo $itm_qty."<br>".$hidden_id."<br>";
}

<input type='text' name='txtQty' value='".$rows['totalInCart']."'>
<input type='hidden' name='hidden_id' value='".$rows['itm_id']."'>
<button type='submit' name='btnUpdate'>Update</button>

Thanks to everyone to give me solution or advise.

Recommended Answers

All 9 Replies

These hidden and button names are repeated means duplicated, so your submit button takes last updated value..

Solution1:

try to concatenate your button , hidden names with itemcode or for loop iterator.

<input type='text' name='txtQty".$rows['itm_id']."' value='".$rows['totalInCart']."'>
<input type='hidden' name='hidden_id".$rows['itm_id']."' value='".$rows['itm_id']."'>

Solution2 :

Write onclick function for update button and pass wanted values in this function and pass that values:

<input type='text' name='txtQty' value='".$rows['totalInCart']."'>
<input type='hidden' name='hidden_id' value='".$rows['itm_id']."'>
<button type='submit' name='btnUpdate' onclick="update_value('".$rows['itm_id']."','".$rows['totalInCart']."');">Update</button>

<script language="javascript">
function assing_value(id,total)
{
   window.href="youyrpage.php?id="+id="&total="+total;
}
</script>

then do your php coding by getting above querystrings like $_GET,$_GET.

if(isset($_GET["id"])){
$itm_qty = $_GET["total"];
$hidden_id = $_GET["id"];
echo $itm_qty."<br>".$hidden_id."<br>";

}

These hidden and button names are repeated means duplicated, so your submit button takes last updated value..

Solution1:

try to concatenate your button , hidden names with itemcode or for loop iterator.

<input type='text' name='txtQty".$rows['itm_id']."' value='".$rows['totalInCart']."'>
<input type='hidden' name='hidden_id".$rows['itm_id']."' value='".$rows['itm_id']."'>

Thanks Shanti,

For solution 1, how those concatenated name 'txtQty00010' and id 'hidden_id00010' get/post once submission? :-/

write $_GET and $_POST in for loop as you wrote for your cart items.
Then you can get your values like:

for()
{
echo $_POST[txtqty.$i];
echo $_POST[hidden_id.$i];

}

or try solution2 which is easier than previous...

Thanks.

for solution 2, I have problem to achieve 'txtQty' values through the loop, like previous case its always pointed to the first textbox value despite i update the second or third row.

From the url value passing, i always get the value '1' (from item K001) in qty somehow i was enter '10' for third(J001) item.

&itm=00026,7&qty=1
function assignValue(itemId,itemQty){
    var qty = document.getElementById('txtQty').value
	
    self.location = '?item='+itemId+'&quantity='+qty;
}

//////

<input type='text' name='txtQty' id='txtQty' value='".$rows['totalInCart']."'>

Any idea?

Thanks.

No,

onclick="update_value('".$rows['itm_id']."','".$rows['totalInCart']."');"

why because the update button also repeated in for loop. Then no problem occur.
you just alert your values in js function and see if they are correct or not by clicking different update buttons.

i think you should get correct values..

No,

onclick="update_value('".$rows['itm_id']."','".$rows['totalInCart']."');"

why because the update button also repeated in for loop. Then no problem occur.
you just alert your values in js function and see if they are correct or not by clicking different update buttons.

i think you should get correct values..

<script type="text/javascript">
function updateValue(itemId,itemQty){
       var maxNum = "<?php echo $nCount; ?>";
	
	for(var i=1; i<=maxNum; i++){
		var qty = document.getElementById('txtQty_'+i).value;
	}
	
	alert('itm='+itemId+'&qty='+qty);
}
</script>

Assume the variable maxNum is '4' below is similar like above in simplicity,

var qty = 'txtQty_1';
var qty = 'txtQty_2';
var qty = 'txtQty_3';
var qty = 'txtQty_4';

document.write("The number is " + qty);

the 'qty' is always grab 'txtQty_4', i did know this is what the problem existed.

I really need a solution for this indeed.

Thanks.

My friend u have not used the PHP tags

<input type='text' name='txtQty' value='".$rows."'>
<input type='hidden' name='hidden_id' value='".$rows."'>

It should be like this i.e use php tags before using any php functions


<input type='text' name='txtQty' value=<?php '".$rows."' ?> >
<input type='hidden' name='hidden_id' value=<?php '".$rows."' ?> >

Thanks Shanki87, its actually already inside PHP tag where is assigned into

<?php
$output = "<input type='text' name='txtQty' value='".$rows."'>
<input type='hidden' name='hidden_id' value='".$rows."'>";

?>

post necessary code we will check it out..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.