Forum Moderators: coopster

Message Too Old, No Replies

Updating Quantity.

Stuck on hopefully solvable problem.

         

Radium

6:20 pm on Nov 2, 2005 (gmt 0)

10+ Year Member



I'm in the process of playing around with a shopping cart and I've got an issue with updating the quantity of a single product in the user's cart. Any help would be greatly appreciated, I've tried searching for an answer but no answer yet so I've come to you. Now on to the question...

I want the user to be able to update the quantity of each item using this:

<input type="text" size="10" name="qty" value="<?php echo $row['qty'];?>" /> <a href="cart.php?action=update_item&id=<?php echo $row["itemId"];?>&qty=?">Update</a>

When the user clicks on Update I want the value of the text input field to be inserted into the "&qty=?" of the Update link. I thought there might be some javascript that I could use to achieve this but I have yet to find it. Thanks again!

-Radium

chriswragg

7:05 pm on Nov 2, 2005 (gmt 0)

10+ Year Member



What I have found sort of does what you have asked, although the javascript does not change the link href in the <a> tag, but actually redirect the user to the page. I figured that clicking on the link would still redirect so I don't think it will make any difference. Tested in Firefox and IE:

In <head>:

<script language="javascript" type="text/javascript">
function updateLink() {
var qty = document.update.qty.value; //Get the form field 'qty' in form 'update'
var url = 'cart.php?action=update_item&id=&qty='; //Set the url it is to be added to
var total = url + qty; //Combine into the final URL
location.href = total; //Redirect User
}
</script>

Link Code:

<form name="update"><input type="text" size="10" name="qty" value="<?php echo $row['qty'];?>" /></form><a href="javascript:updateLink();">Update</a>


The form tag is simply so the javascript can get the input field value

You may need to change some tags to make them XHTML copliant, but I use HTML 4.01 so I don't know what to change!

Hope this helps

Chris

Radium

7:28 pm on Nov 2, 2005 (gmt 0)

10+ Year Member



Awesome! Thank you, this is a good start. One problem though, it does not work while multiple products are in the cart.

I tried this but I don't know how to get the variable itemId to insert after qty_

document.frmCart.qty_.value;

In Head:

<script type="text/javascript">
function updateLink(itemId) {
var qty = document.frmCart.qty_itemId.value; // Get the form field 'qty' in form 'update'
var url = 'cart.php?action=update_item&id='+ itemId +'&qty='; // Set the url it is to be added to
var total = url + qty; // Combine into the final URL
location.href = total; // Redirect User
}
</script>

In Body:

<input type="text" size="10" name="qty_<?php echo $row["itemId"];?>" value="<?php echo $row['qty'];?>" /> <a href="javascript:updateLink('<?php echo $row["itemId"];?>');">Update</a>

chriswragg

8:15 pm on Nov 3, 2005 (gmt 0)

10+ Year Member



Just insert the PHP code into the javascript:

<script type="text/javascript">
function updateLink(itemId) {
var qty = document.frmCart.qty_<?php echo $row["itemId"];?>.value;
var url = 'cart.php?action=update_item&id='+ itemId +'&qty=';
var total = url + qty;
location.href = total;
}
</script>

Chris

Peb0

8:23 pm on Nov 3, 2005 (gmt 0)

10+ Year Member



I don't know php but I know scripting and html....
Are all your "qty" input fields named uniquely?

If all of them are named "qty" I'm sure that's the problem you are experiencing when there are more than one item in your cart.

Try naming the input fields "qty<ProdID>" or something...

PebĪ

chrisjoha

12:04 am on Nov 4, 2005 (gmt 0)

10+ Year Member



Allthough what you are trying to achieve is a good idea, it fails badly whithout javascript... You should generate a normal submit button and then use javascript to replace this with alink. This way users without javascript get a functional page, and user with javascript get an enhanced verison - just the way it should be :)

Radium

7:02 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



Thanks for all of your help, I've got it working now. What I ended up doing is this for each product in the cart:

<form name="update_item" method="get">
<input type="hidden" name="action" value="update_item" />
<input type="hidden" name="id" value="<?php echo $row['itemId'];?>" />
<input type="text" size="6" name="qty" value="<?php echo $row['qty'];?>" /> <input type="submit" value="Update" /></a>
</form>

The URL winds up like this:

cart.php?action=update_item&id=1&qty=200

Works perfectly! :)