SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Display result without refresh

    Hello,

    I am trying to update my PHP CART without having the page refreshed!
    I don't know how to do it because just now I got JS/AJAX/JQuery to play with.

    So any help will be appriciated.
    (I already tried some scripts but.... )

    Here is a part of my code... I will just use the UPDATE_QUANTITY part (there are ADD & Remove)

    My form that holds the quantity:
    Code:
    <form name="upd_itm" id="upd_itm" action="cart_function.php" method="post">
    <span class="text"><input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" style="text-align: center;"/>
    <br />
    <button name="adjustBtn' . $item_id . '" id="adjustBtn' . $item_id . '" type="submit" class="avoid_ref" value=""><img src="bttn_pics/change_value.png" /></button>
    <input name="item_to_adjust" id="item_to_adjust" type="hidden" value="' . $item_id . '" /></span>
    </form>
    My action.php that proccess it:
    Code:
    if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
        // execute some code	$item_to_adjust = $_POST['item_to_adjust'];	$quantity = $_POST['quantity'];
    	$quantity = preg_replace('#[^0-9]#i', '', $quantity);
     // filter everything but numbers
          if ($quantity >= 100) { $quantity = 99; }
          if ($quantity < 1) { $quantity = 1; }
          if ($quantity == "") { $quantity = 1; }
            $i = 0;
             foreach ($_SESSION["cart_array"] as $each_item) {
     	$i++;
    	while (list($key, $value) = each($each_item)) {
    	  if ($key == "item_id" && $value == $item_to_adjust) {	
    	  // That item is in cart already so let's adjust its quantity using array_splice()
    	  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
    	  } // close if condition
          } // close while loop
    } // close foreach loop}
    Thank you in advance

  2. #2
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if you're using jquery, it could go something like this:

    Code:
    $(document).ready(function(){
      $("#upd_itm").submit(function(e){ // e is the form submit event object
        e.preventDefault(); // prevent the form from submitting normally
        $.ajax({
          type: 'POST'
          url: 'myPHPpage.php',
          data: 'quantity=' + $("#quantity").val() + '&item_to_adjust=' + $("#item_to_adjust").val(),
          success: function () {
            doSomething();
          }
        });
      });
    
      function doSomething() {
        // insert code to do something on successful ajax response
      }
    });
    Make sure to add an id field to the quantity input and change the 'myPHPpage.php' to whatever page you're submitting to...

  3. #3
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Thanks for the reply. I just saw it.

    So I will change the url to my cart_functions.php (which is the form's action="") and I will ad an id=quantity to my text field.

    I will give it a try later and I will get back to you.

    Thanks again for the reply.

  4. #4
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello,

    It "seems" to work (I cannot explain why it seems at the moment) but if it does its working only for the first item in cart.

  5. #5
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to have unique Id place holder

    <div id='my_cart_info'></div>

    On first page request - Use PHP to display cart info.

    On addition/update to cart - do Ajax CALL to your server side script
    1. Do cart processing
    2. Run the same PHP function to get current cart content and echo it.
    3. Get the HTML and set it from AJAX function.

    I hope it's clear to you.

  6. #6
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    You need to have unique Id place holder
    My cart <div id="is_unique">. Always has been

    On first page request - Use PHP to display cart info.
    I have my aaa.php script to handle cart for first page request. If I didn't I wouldn't have had a cart

    Do cart processing
    Isn't what my aaa.php is doing??? Do I need s/thing else?

    Run the same PHP function to get current cart content and echo it.
    This is how my cart is working anyway.

    Get the HTML and set it from AJAX function.
    And This is WHERE I TOTALLY FAIL!!!! (I am not very familiar using AJAX.)

    So if any solution is available and working...

    Thank you


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
  •