SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Forms when using PHP: Update Basket

    Hi,

    I have written code for a basket page. I am using Switch functions for:

    add item - adds to basket and also checks if already in basket - if in basket already increases quantity.

    remove item - button on basket page to delete that product from the basket.

    update item - If a customer wants to change the quantity.

    I have a problem with the update item. This is the code suggested to me:

    PHP Code:
    function UpdateItem($ID$quantity){

    //It executes a simple UPDATE SQL query against the cart table, updating the quantity of one specific item. The cookieId //field is used to match the users session ID to that particular product, making sure that the quantity is only updated for //that item and the current user:


    @mysql_query("update cart set quantity = quantity + '$quantity' where cookieid = '" GetCartId() . "' and productid = $ID"); 
    I cannot get it to work. I thought I might need to use a form to submit the quantity that the user has entered into the 'quantity' box. If the new quantity entered into the box can be submitted using PHP then i would prefer that.

    How do I use the quantity entered by the customer to get the quantity updated?!?

    It might be that the code above is correct but I do not have the right code on the basket page to use that code. I have a text box on the basket page but I am not entirely sure how to initiate the code above.

    Matt.

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As suggested to you in this thread, echo out the query to see what is actually being run and post it if you can't see any error.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but my question is can it be done with PHP or must it be a form?

    There is a text box on the basket page. The customer could delete quantity 1 and write in 3. When they click update basket can this be updated using PHP or must it be done with a form!?!

    If it cannot be done with PHP what is the code above suggesting? The bit I do not think PHP can do is take a value from a text box!?

    Matt.

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure exactly how you're doing this, but it sounds messy.

    The way I would suggest doing this is to build a php class to be your basket. The basket class will have properties (arrays) for quantitiy and prices. Both arrays would have the product ID as the key and one array will store the quantities for each item and the other array would store the prices for each item.

    The basket class would have methods to add/delete/edit items and quantities and to calculate the total value of the basket.

    When a user clicks something or enters a value in a text box to modify the contents of the cart, the appropriate class method is called to perform the task. Now whether you use forms or not is up to you, but the cart has to be maintained server side.

  5. #5
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post
    The bit I do not think PHP can do is take a value from a text box!?
    PHP code is executed server side and values are entered in a text box client side so obviously php can't take a value from a text box.

    You have to send the value in the text box to the php script on the server. You can do that with AJAX or append the value to the php script's url via a query string or submit the value via a form.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •