SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    insert values from indexed array into html form button (PHP)

    I'm designing an online shop - http://growinggifts.co.uk/shop.php . It has a functioning shopping basket coded with PHP, using a mysql db. However I'd like to create an "add to Paypal cart button", which passes the entire basket contents (item names, prices, quantity) to paypals cart, with one click. The shoppingcart.php file (shown further down) returns an indexed array of all the contents in the basket using the function "get_contents". It is then displayed in basket.php using a table. I'd like to have this "add to paypal cart" in basket.php, below the table of basket contents.
    Each item to be added to paypals shopping cart requires 4 lines of form code within the "add to cart" button.
    eg.
    <input type='hidden' name='item_name_1' value='Medium Ceramic Pot' />
    <input type='hidden' name='amount_1' value='18.00' />
    <input type='hidden' name='currency_code' value='GBP' />
    <input type='hidden' name='quantity_2' value = '2' />
    The item_name_? and amount_? must be ascending 1, 2, 3, 4 etc.. The quantity_? and its value is generated from the current basket contents.

    Below is all the relevant code. Can anyone show me the code to do this?

    First of all ...this is basically what I'm trying to achieve...
    HTML Code:
    <form target="paypal" action="https://www.paypal.com/uk/cgi-bin/webscr" method="post">
         <input type='hidden' name='cmd' value='_cart' /> 
         <input type='hidden' name='upload' value='1' /> 
         <input type='hidden' name='business' value='email' /> 
    
         //need to automatically generate these (example) lines, getting the values from the current basket contents
         //item_name_? and amount_? must be ascending 1, 2, 3, 4 etc..
         //quantity_? and its value is generated from the current basket contents
         //*******************************************************************
            <input type='hidden' name='item_name_1' value='Medium Ceramic Pot' /> 
         <input type='hidden' name='amount_1' value='18.00' /> 
         <input type='hidden' name='currency_code' value='GBP' />
         <input type='hidden' name='quantity_2' value = '2' />     
         //*******************************************************************
         <input type='hidden' name='item_name_2' value='Small Ceramic Pot' /> 
         <input type='hidden' name='amount_2' value='14.00' /> 
         <input type='hidden' name='currency_code' value='GBP' />
         <input type='hidden' name='quantity_1' value = '1' />     
         //*******************************************************************
         <input type='hidden' name='item_name_3' value='Small Hanging Basket' /> 
         <input type='hidden' name='amount_3' value='15.00' /> 
         <input type='hidden' name='currency_code' value='GBP' />
         <input type='hidden' name='quantity_1' value = '1' />     
         //*******************************************************************
         etc...
    PHP Code:
    basket.php
    *******************************************************************
    <? 
    // Cart Cookie Creator 
    if(!isset($HTTP_COOKIE_VARS['cart_id'])) { 
         
    $cart_id md5(uniqid(rand())); 
         
    setcookie("cart_id"$cart_idtime() + 14400); 
     } else { 
          
    $cart_id $HTTP_COOKIE_VARS['cart_id']; 
     }
    // Make the Cart Object 
    require_once("shoppingcart.php");
    $cart = new cart($cart_id);
    // Select Action 
    switch($_GET['a']){     
         case 
    "add":
              if(isset(
    $_POST['prodId']) and isset($_POST['quantity'])) $cart->add_item($_POST['prodId'],$_POST['quantity']);
              break;
              
         case 
    "update":
              
    $cart->modify_quantity($_POST["prodId"],$_POST["quantity"]);
              break;
         
         case 
    "delete":
              
    $cart->delete_item($_POST['prodId']);
              break;
              
         case 
    "clear":
              
    $cart->clear_cart();
              break;
    }
    // Begin Cart Display 
    $cartContents $cart->get_contents();
    $cart_tbl "
         <!-- Begin Cart Display -->
         <table summary='Your shopping cart' id='cart'>
              <tr>
                   <th>Item</th>
                   <th>Product Name</th>
                   <th>Quantity</th>
                   <th>Price</th>
                   <th>&nbsp;</th>
              </tr>"
    ;
    if(
    count($cartContents) <= 0){ //nothing in cart
         
    $cart_tbl .= "
              <tr>
                   <td class='items' colspan='5'>Your cart is empty</td>
              </tr>"
    ;
                          
    }else{ 
    //stuff in the cart so show it
         
    for($i=0$i count($cartContents); $i++){
              
    $cart_tbl .= "
              <tr>
                   <td class='items'>" 
    $cartContents[$i][0] . "</td>
                   <td class='items'>" 
    $cartContents[$i][1] . "</td>
                   <td class='items'>
                   <form action=\"basket.php?a=update\" method=\"POST\">
                        <input type='hidden' name='prodId' value='"
    $cartContents[$i][0] ."' />
                        <input type='text' name='quantity' size='3' value='" 
    $cartContents[$i][2] . "' /> 
                        <input type='submit' value='update' />
                   </form>
                   </td>
                   <td class='items'>"
    $cartContents[$i][3] ."</td>
                   <td class='items'>
                   <form action=\"basket.php?a=delete\" method=\"POST\">
                        <input type='hidden' name='prodId' value='"
    $cartContents[$i][0] ."' />
                        </>
                        <input type='submit' value='remove item'  />
                   </form>
                   </td>
              </tr>"
    ;}}     
         
    $cart_tbl .= "
              <tr>
                   <td colspan='3' class='empty'>&nbsp;</td><td class='total'>"
    $cart->cart_total() ."</td>
    <td class='items'><form action=\"basket.php?a=clear\" method=\"post\"><input type=\"submit\" value=\"clear cart\" /></form></td>     
              </tr>"
    ;
         
    $cart_tbl .= "
              </tr>
         </table>
         <!-- End Cart Display -->\n\n\n"
    ;                                   


    //add all basket contents to paypal cart (the bit I'm stuck on)

         
    <form target="paypal" action="https://www.paypal.com/uk/cgi-bin/webscr" method="post">
         <
    input type='hidden' name='cmd' value='_cart' /> 
         <
    input type='hidden' name='upload' value='1' /> 
         <
    input type='hidden' name='business' value='email' /> 

         
    //need to automatically generate these (example) lines, getting the values from the current basket contents
         //item_name_? and amount_? must be ascending 1, 2, 3, 4 etc..
         //quantity_? and its value is generated from the current basket contents
         //*******************************************************************
            
    <input type='hidden' name='item_name_1' value='Medium Ceramic Pot' /> 
         <
    input type='hidden' name='amount_1' value='18.00' /> 
         <
    input type='hidden' name='currency_code' value='GBP' />
         <
    input type='hidden' name='quantity_2' value '2' />     
         
    //*******************************************************************
         
    <input type='hidden' name='item_name_2' value='Small Ceramic Pot' /> 
         <
    input type='hidden' name='amount_2' value='14.00' /> 
         <
    input type='hidden' name='currency_code' value='GBP' />
         <
    input type='hidden' name='quantity_1' value '1' />     
         
    //*******************************************************************
         
    <input type='hidden' name='item_name_3' value='Small Hanging Basket' /> 
         <
    input type='hidden' name='amount_3' value='15.00' /> 
         <
    input type='hidden' name='currency_code' value='GBP' />
         <
    input type='hidden' name='quantity_1' value '1' />     
         
    //*******************************************************************
         
    etc...

         <
    input type='submit' name='submit' value='Pay via Paypal' /></form>

    ?>
    PHP Code:
    relevant functions to this problem in shoppingcart.php
    *******************************************************************

    // Adapted from John Coggeshall's tutorial at:                                          
    // [url]http://www.zend.com/zend/spotlight/php-shopping-cart1.php[/url]

         
    function get_contents() {
              
              
    //get stuff in the cart and return in indexed array
              
    $q1 "SELECT * FROM "$this->cart_table" WHERE session='".$this->cart_id."'";
              
    $incart_r mysql_query($q1,$this->dblink) or die(mysql_error());
              
              
    $contents = array();
              
              while(
    $incart mysql_fetch_array($incart_r)){
                   
    //get the item's price first
                   
    $q2 "SELECT price, prodName FROM "$this->inv_table" WHERE prodId='".$incart["product"]."'";
                   
    $prodResult mysql_query($q2) or die(mysql_error());
                   
    $name mysql_result($prodResult,0,"prodName");
                   
    $price mysql_result($prodResult,0,"price");
              
                                       
                   
    //build array of info
                   
    $item = array($incart['product'], $name$incart['quantity'],$price);
                   
    array_push($contents,$item);
              }
              
              return 
    $contents;
         } 
         
         
         function 
    quant_items() { 
            
    $contents $this->get_contents();
            
    $q 0;
            for(
    $i=0$i count($contents); $i++){
                   
    $q $q $contents[$i][2];
            }
            return 
    $q;
          } 
    Last edited by boognish; Jan 10, 2006 at 15:51.

  2. #2
    Non-Member jake4974's Avatar
    Join Date
    Jan 2006
    Location
    Down the road, it's where I'll always be
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just give people an option to pay with paypal on checkout? Maybe I don't understand. And after looking at all of that code, I tend to forget what the question is. Try not to put so much code up without wrapping some of the various code tags around it. There's php wrapping, html and just plain code. Makes it much easier to read and will greatly increase the chance of response.

  3. #3
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jake, thanks for the reply. I've formatted the question as requested.
    This is essentially what I'm trying to achieve. I already have an existing shopping cart with its own basket. I'd like to give the customer various options of payment, one of which is paypal. I'd like to pass all the item information - product names, prices and quantities to a paypal shopping basket.
    My problem is - The contents of the basket are returned using an indexed array. I know how display this kind of array with tables, but I don't know how to pass all these seperate values into the appropriate places in form buttons. I just want one single "pay via paypal" button in basket.php, which adds all items in the basket at once to paypals shopping cart. The way to do this is to generate form code of all the product values from the current basket within this form button;
    Like this;
    HTML Code:
    //need to automatically generate these (example) lines, getting the values from the current basket contents
         //item_name_? and amount_? must be ascending 1, 2, 3, 4 etc..
         //quantity_? and its value is generated from the current basket contents
         //*******************************************************************
            <input type='hidden' name='item_name_1' value='Medium Ceramic Pot' /> 
         <input type='hidden' name='amount_1' value='18.00' /> 
         <input type='hidden' name='currency_code' value='GBP' />
         <input type='hidden' name='quantity_2' value = '2' />     
         //*******************************************************************
         <input type='hidden' name='item_name_2' value='Small Ceramic Pot' /> 
         <input type='hidden' name='amount_2' value='14.00' /> 
         <input type='hidden' name='currency_code' value='GBP' />
         <input type='hidden' name='quantity_1' value = '1' />     
         //*******************************************************************
         <input type='hidden' name='item_name_3' value='Small Hanging Basket' /> 
         <input type='hidden' name='amount_3' value='15.00' /> 
         <input type='hidden' name='currency_code' value='GBP' />
         <input type='hidden' name='quantity_1' value = '1' />     
         //*******************************************************************
        // etc...
    
    <input type='submit' name='submit' value='Pay via Paypal' /></form>
    So essentially my question is how do I get the baskets values from the indexed array into the appropiate automatically generated form code within the "pay via paypal" button?

    Thanks!

  4. #4
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out! If anyone wants this for reference -
    Heres the solution;

    for($i=0; $i < count($cartContents); $i++){
    $val = $i + 1;
    $paypalitems .= "
    <input type='hidden' name='item_name_$val' value='". $cartContents[$i][1] ."' />
    <input type='hidden' name='amount_$val' value='". $cartContents[$i][3] ."' />
    <input type='hidden' name='currency_code' value='GBP' />
    <input type='hidden' name='quantity_$val' value = '" . $cartContents[$i][2] . "' />
    ";
    }

    <?
    echo $paypalitems;
    ?>


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
  •