Avatar billede czharina Nybegynder
24. april 2008 - 00:34 Der er 23 kommentarer og
1 løsning

Få min form til at samle et total fra mit script op

Hej alle!

Jeg håber det er ok at en del af mit indlæg er på engelsk. I må gerne skrive på dansk. :) Jeg har spurgt et par steder på nettet, men har ikke fundet en løsning på denne paypal-integrations udfordring endnu. Er der nogen der kan hjælpe her?

I've built a third party shopping cart, click here if you'd like to see it:


http://www.carlinart.net/shoppingcart/index.php


I want to integrate paypal so the aggregate cart amount is passed to paypal.

From the online guides at paypal I can read:

"If you wish, you may aggregate your entire shopping cart and pass the total amount into PayPal's Buy Now Button code (that is, you will need to post a single name for the entire cart and the total price of the cart's contents as though it were a purchase of a single item)."

Is it really the case that you cannot have a price that changes depending on the customers purchases in your shop?

I've used this code:

[IMG]http://img261.imageshack.us/img261/4329/billede1mc1.jpg[/IMG]

The field

<input type="hidden" name="amount_1" value="5.00"/>

is what seems interesting. How can I have the form read the total calculation of my cart, and not depend on, say, a 5 dollar price that's fixed?

Could I use a  $_POST or $_GET variable in some sneaky fashion to have the form pick up on the calculated total?


I use a MySql database to describe my products. (right now they're only test books. :) ) If I have to find an alternate solution because the form can't somehow be made to read the total of the shopping cart, I'd choose the second method of passing individual items to payPal.

I need some help with this, though. It means my script will have to generate details about each product to upload to paypal? I use Dreamweaver and would greatly appreciate an example of this type of script. Here is what my original script for the shopping cart looks like:

[IMG]http://img261.imageshack.us/img261/209/billede2jv0.jpg[/IMG]

Also, do I need to edit my product details in the database? Right now they look like this:

[IMG]http://img518.imageshack.us/img518/6493/billede3xr9.jpg[/IMG]

Could someone give an example of which details need to be edited and what to?


Jeg har senere hen prøvet at få $total over i Paypals informationer ved at echoe det ind i inputtets value. Ala her:

<input type="hidden" name="amount_1" value="<?php echo $total; ?>"/>


Men når man skriver andet end et tal (fx 1.00) i valueen, generer paypal en fejl der siger shopping cart er tom.

Men jeg er overbevist om at der må være en måde at få min carts total over i paypal formen. Er der nogen der kender noget til det?

På forhånd tak for hjælpen!
Avatar billede w13 Novice
24. april 2008 - 09:41 #1
Så du siger, at det ikke er et tal, der bliver skrevet, når du bruger:

<input type="hidden" name="amount_1" value="<?php echo $total; ?>"/>

Hvad er det så, der bliver indsat, hvis det ikke er et tal?
Avatar billede czharina Nybegynder
24. april 2008 - 12:10 #2
Ah, det er bare et eksempel på noget jeg har prøvet, det virker desværre ikke med php echo. Eller hvis man skriver andet end 1.00/2.00/3.00 i det felt. Så kommer fejlen i paypal.
Avatar billede w13 Novice
24. april 2008 - 12:12 #3
Og hvad er det, du gerne ville udskrive, som ikke virker?
Avatar billede czharina Nybegynder
24. april 2008 - 12:22 #4
Jeg vil gerne have sum total fra shopping carten til at blive samlet op af paypals form. Pt, når man klikker på paypal knappen, skriver den en statisk pris (som man har indsat ved 1.00 eller andet tal). Men mit script fra indkøbskurven beregner jo priser afhængig af hvad kunderne gerne vil købe - og det er så den samlede beregning fra det script ($total) jeg vil have til at erstatte det statiske tal i:

<input type="hidden" name="amount_1" value="1.00"/>
Avatar billede w13 Novice
24. april 2008 - 12:24 #5
Jep, det er jeg med på. Men du sagde at det ikke virkede, så derfor kunne det være sjovt at se, hvad du havde prøvet at indsætte i amount_1.
Avatar billede czharina Nybegynder
24. april 2008 - 12:34 #6
Ahh, jeg har prøvet den slags her:

<input type="hidden" name="amount_1" value="$total">

Jeg har jo på et seperat phpark kodningen til shopping kurven, hvor den endelige pris repræsenteres af $total. Udfordringen ligger så i hvordan value kan skrives så den lytter efter $total og sender informationen videre i paypal.
Avatar billede w13 Novice
24. april 2008 - 12:38 #7
Hvad bliver der udskrevet, når du kører:

<?php echo $total; ?>

?
Avatar billede czharina Nybegynder
24. april 2008 - 12:46 #8
Så reagerer paypal på den måde, at den fortolker indkøbskurven som tom. Det siger den på ALT som ikke er et 1.00 tal. Meget mærkeligt! Er det mon paypal som fra deres side afviser udregningen, eller kan man stadig få det til at virke hvis man kender den rigtige måde at formulere det på?
Avatar billede w13 Novice
24. april 2008 - 13:03 #9
Men kan du ikke lige gå ind i kildekoden og aflæse, hvad koden rent faktisk udskriver.
Avatar billede 44fire44 Nybegynder
24. april 2008 - 13:09 #10
hvis det er fordi at det som bliver sendt til <?php echo $total; ?> ikke er 1.00 men "1" eller "1,00" så må du bruge str_replace og skifte , ud med 1 eller ilføje .00 til sidst
Avatar billede czharina Nybegynder
24. april 2008 - 13:12 #11
Ah, jo. Den siger faktisk:

<div id="apDiv7">
  <form action="https://www.paypal.com/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="carina2b@hotmail.com"/>
<input type="hidden" name="item_name_1" value="Grand total"/>
<input type="hidden" name="amount_1" value=""/>
<input type="submit" value="PayPal">

</form> </div>

Så der kan man se valuen helt forsvinder..
Avatar billede czharina Nybegynder
24. april 2008 - 13:17 #12
"hvis det er fordi at det som bliver sendt til <?php echo $total; ?> ikke er 1.00 men "1" eller "1,00" så må du bruge str_replace og skifte , ud med 1 eller ilføje .00 til sidst"

Jeg tror faktisk det godt kunne være det!

Jeg sidder og kigger på scriptet for shopping carten, kan du hjælpe mig med hvor .00 skal sættes ind?

function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
        $output[] = '<table>';
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM books WHERE id = '.$id;
            $result = $db->query($sql);
            $row = $result->fetch();
            extract($row);
            $output[] = '<tr>';
            $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
            $output[] = '<td>'.$title.' by '.$author.'</td>';
            $output[] = '<td>&pound;'.$price.'</td>';
            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
            $output[] = '<td>&pound;'.($price * $qty).'</td>';
            $total += $price * $qty;
            $output[] = '</tr>';
        }
        $output[] = '</table>';
        $output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>';
        $output[] = '<div><button type="submit">Update cart</button></div>';
        $output[] = '</form>';
    } else {
        $output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
   
   
}
Avatar billede w13 Novice
24. april 2008 - 13:25 #13
Nej, problemet er jo, at der slet ikke er adgang til beløbet gennem variablen $total.

Er der meget andet kode på din side med:

<div id="apDiv7">
  <form action="https://www.paypal.com/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="carina2b@hotmail.com"/>
<input type="hidden" name="item_name_1" value="Grand total"/>
<input type="hidden" name="amount_1" value=""/>
<input type="submit" value="PayPal">

</form> </div>

eller kan vi se det?
Avatar billede czharina Nybegynder
24. april 2008 - 13:32 #14
Der er ikke andet kode fra paypal end den der bid, som supplerer paypal købeknappen. I må meget gerne se koden fra mine sider:

Her er shopping cart script koden:

<?php
function writeShoppingCart() {
    $cart = $_SESSION['cart'];
    if (!$cart) {
        return '<p>You have no items in your shopping cart</p>';
    } else {
        // Parse the cart session variable
        $items = explode(',',$cart);
        $s = (count($items) > 1) ? 's':'';
        return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
    }
}

function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
        $output[] = '<table>';
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM books WHERE id = '.$id;
            $result = $db->query($sql);
            $row = $result->fetch();
            extract($row);
            $output[] = '<tr>';
            $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
            $output[] = '<td>'.$title.' by '.$author.'</td>';
            $output[] = '<td>&pound;'.$price.'</td>';
            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
            $output[] = '<td>&pound;'.($price * $qty).'</td>';
            $total += $price * $qty;
            $output[] = '</tr>';
        }
        $output[] = '</table>';
        $output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>';
        $output[] = '<div><button type="submit">Update cart</button></div>';
        $output[] = '</form>';
    } else {
        $output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
   
   
}



?>

Her er koden fra den side med her:


http://www.carlinart.net/shoppingcart/cart.php?action=add&id=3


<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

<h1>Please check quantities...</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Back to bookshop...</a></p>
<p>&nbsp;</p>


</div>


<div id="apDiv7">
  <form action="https://www.paypal.com/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="carina2b@hotmail.com"/>
<input type="hidden" name="item_name_1" value="Grand total"/>
<input type="hidden" name="amount_1" value="<?php echo $total; ?>"/>
<input type="submit" value="PayPal">

</form> </div>
Avatar billede w13 Novice
24. april 2008 - 13:36 #15
Prøv at rette følgende side: http://www.carlinart.net/shoppingcart/cart.php?action=add&id=3

til:
-------------------------------------------

<body>

<?php
function getTotalPrice() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM books WHERE id = '.$id;
            $result = $db->query($sql);
            $row = $result->fetch();
            extract($row);
            $total += $price * $qty;
        }
    }
    return join('',$total);
}
?>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

<h1>Please check quantities...</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Back to bookshop...</a></p>
<p>&nbsp;</p>


</div>


<div id="apDiv7">
  <form action="https://www.paypal.com/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="carina2b@hotmail.com"/>
<input type="hidden" name="item_name_1" value="Grand total"/>
<input type="hidden" name="amount_1" value="<?php echo getTotalPrice(); ?>"/>
<input type="submit" value="PayPal">

</form> </div>
Avatar billede w13 Novice
24. april 2008 - 13:36 #16
Problemet er jo, at der ikke er noget på siden, som _finder_ prisen, men det er der så i kraft af den funktion, jeg har sat ind.
Avatar billede czharina Nybegynder
24. april 2008 - 13:37 #17
Jeg tror måske godt str_replace kunne være løsningen. Hvilken side skal jeg inkludere det på - shopping script siden, eller den side hvor paypalknappen findes? Ville blive meget glad hvis jeg kunne få koden formuleret!
Avatar billede w13 Novice
24. april 2008 - 13:39 #18
Da du udskrev $total så vi jo, at den var helt tom, så der er ikke noget at replace på.

Men prøv lige min kode.
Avatar billede czharina Nybegynder
24. april 2008 - 13:43 #19
Tak for forslaget w13! Den siger nu: please enter an amount greater than zero, på paypals side. I kilden står der:

<div id="apDiv7">
  <form action="https://www.paypal.com/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="carina2b@hotmail.com"/>
<input type="hidden" name="item_name_1" value="Grand total"/>
<input type="hidden" name="amount_1" value="<br />
<b>Warning</b>:  join() [<a href='function.join'>function.join</a>]: Bad arguments. in <b>/data/web/carlinartnet/carlinart.net/shoppingcart/cart.php</b> on line <b>206</b><br />
"/>
<input type="submit" value="PayPal">

</form> </div>


På linje 206 i dreamweaver står der:

return join('',$total);

Der var i øvrigt en lille bid kode på cart siden jeg ikke pastede før, den ser sådan her ud:

// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
    case 'add':
        if ($cart) {
            $cart .= ','.$_GET['id'];
        } else {
            $cart = $_GET['id'];
        }
        break;
    case 'delete':
        if ($cart) {
            $items = explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) {
                if ($_GET['id'] != $item) {
                    if ($newcart != '') {
                        $newcart .= ','.$item;
                    } else {
                        $newcart = $item;
                    }
                }
            }
            $cart = $newcart;
        }
        break;
    case 'update':
    if ($cart) {
        $newcart = '';
        foreach ($_POST as $key=>$value) {
            if (stristr($key,'qty')) {
                $id = str_replace('qty','',$key);
                $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                $newcart = '';
                foreach ($items as $item) {
                    if ($id != $item) {
                        if ($newcart != '') {
                            $newcart .= ','.$item;
                        } else {
                            $newcart = $item;
                        }
                    }
                }
                for ($i=1;$i<=$value;$i++) {
                    if ($newcart != '') {
                        $newcart .= ','.$id;
                    } else {
                        $newcart = $id;
                    }
                }
            }
        }
    }
    $cart = $newcart;
    break;
}
$_SESSION['cart'] = $cart;
?>
Avatar billede czharina Nybegynder
24. april 2008 - 13:46 #20
Så altså, nu ser hele koden for denne her side http://www.carlinart.net/shoppingcart/cart.php?action=add&id=3 sådan her ud:


<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
    case 'add':
        if ($cart) {
            $cart .= ','.$_GET['id'];
        } else {
            $cart = $_GET['id'];
        }
        break;
    case 'delete':
        if ($cart) {
            $items = explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) {
                if ($_GET['id'] != $item) {
                    if ($newcart != '') {
                        $newcart .= ','.$item;
                    } else {
                        $newcart = $item;
                    }
                }
            }
            $cart = $newcart;
        }
        break;
    case 'update':
    if ($cart) {
        $newcart = '';
        foreach ($_POST as $key=>$value) {
            if (stristr($key,'qty')) {
                $id = str_replace('qty','',$key);
                $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                $newcart = '';
                foreach ($items as $item) {
                    if ($id != $item) {
                        if ($newcart != '') {
                            $newcart .= ','.$item;
                        } else {
                            $newcart = $item;
                        }
                    }
                }
                for ($i=1;$i<=$value;$i++) {
                    if ($newcart != '') {
                        $newcart .= ','.$id;
                    } else {
                        $newcart = $id;
                    }
                }
            }
        }
    }
    $cart = $newcart;
    break;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>PHP Shopping Cart Demo &#0183; Cart</title>
    <link rel="stylesheet" href="css/styles.css" />
    <style type="text/css">
<!--
#apDiv6 {
    position:absolute;
    left:363px;
    top:680px;
    width:382px;
    height:86px;
    z-index:11;
}
#apDiv7 {
    position:absolute;
    left:363px;
    top:628px;
    width:265px;
    height:85px;
    z-index:11;
}
#apDiv8 {
    position:absolute;
    left:652px;
    top:654px;
    width:96px;
    height:51px;
    z-index:12;
}
-->
    </style>
</head>

<style type="text/css">
<!--
body {
    margin-left: 0px;
    background-repeat: repeat-y;
    background-color: #3C497D;
}
#Layer2 {
    position:absolute;
    left:11px;
    top:773px;
    width:1003px;
    height:94px;
    z-index:2;
}
#apDiv1 {
    position:absolute;
    left:26px;
    top:199px;
    width:146px;
    height:47px;
    z-index:3;
}
#apDiv2 {
    position:absolute;
    left:118px;
    top:268px;
    width:116px;
    height:143px;
    z-index:4;
}
.style29 {
    color: #5D6A7D;
    font-weight: bold;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 14px;
}
.style32 {
    color: #646A7D
}
a:link {
    color: #5D6A7D;
    text-decoration: none;
}
a:visited {
    text-decoration: none;
    color: #5D6A7D;
}
a:hover {
    text-decoration: none;
}
a:active {
    text-decoration: none;
}
#apDiv3 {
    position:absolute;
    left:297px;
    top:172px;
    width:81px;
    height:36px;
    z-index:5;
}
.style36 {color: #5D6A7D}
#apDiv4 {
    position:absolute;
    left:258px;
    top:244px;
    width:729px;
    height:244px;
    z-index:6;
}
#apDiv5 {
    position:absolute;
    left:256px;
    top:500px;
    width:735px;
    height:260px;
    z-index:7;
}
-->
</style>

<body>

<?php
function getTotalPrice() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM books WHERE id = '.$id;
            $result = $db->query($sql);
            $row = $result->fetch();
            extract($row);
            $total += $price * $qty;
        }
    }
    return join('',$total);
}
?>


<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

<h1>Please check quantities...</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Back to bookshop...</a></p>
<p>&nbsp;</p>


</div>


<div id="apDiv7">
  <form action="https://www.paypal.com/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="carina2b@hotmail.com"/>
<input type="hidden" name="item_name_1" value="Grand total"/>
<input type="hidden" name="amount_1" value="<?php echo getTotalPrice(); ?>"/>
<input type="submit" value="PayPal">

</form> </div>





</div>
<div id="bluebackground2"/>
<h1>&nbsp;</h1>
<div id="apDiv1">
  <p align="left" class="style29">Carlinart Index</p>
</div>
<div id="apDiv2">
  <h4 align="center" class="style32"><a href="../home.html">Home</a><br />
    <a href="../art.html">Art</a><br />
    <a href="../artist.html">Artist</a><a href="../prints.html"><br />
  Prints</a></h4>
  <h4 align="center" class="style32"><a href="../commissions.html">Commission</a> <a href="../journal.html">Journal</a><br />
    <a href="../contact.html">Contact</a></h4>
</div>
</body>
</html>
Avatar billede w13 Novice
24. april 2008 - 13:47 #21
Nå ja, ret lige:

    return join('',$total);

i mit kodeeksempel til:

    return $total;

Doh!
Avatar billede czharina Nybegynder
24. april 2008 - 13:51 #22
OMFG!! Det virker. 0_0 Er det virkelig sandt! Jeg sidder lige i shock.
Avatar billede w13 Novice
24. april 2008 - 13:53 #23
:D

Så opretter jeg da lige et svar. ;)
Avatar billede czharina Nybegynder
24. april 2008 - 14:01 #24
Ja, det virker sgu. DU ER ET GENI!!! Jeg sidder lige og tygger på den. Der er stadig nogle smådetaljer, som: kan jeg få scriptet til at sende mig en mail med hvad folk har købt, hvis denne information går tabt når den eneste information paypal opsamler er "Grand total"? Eller mon scriptets beskrivelser af varerne blever udsendt i en paypal confirmation email?

Tak, tak, tak for hjælpen indtil videre.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester