Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help completing items in cart script

Status
Not open for further replies.

lukefred76

Technical User
Apr 29, 2008
11
0
0
GB
Hi,

I'm working on a sml shop basket and need some help with a bit of code.

I have a simple form which allows users to select thedocs they wish to purchase and if they wnt to purchase a subscription service...

Code:
<form method="POST" name="selectionForm"  action="<?php echo $PHP_SELF; ?>" >
<table class="pandp" width="80%" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td width="447"><strong>Doc</strong></td>
    <td width="30"><strong>Price</strong></td>
    <td width="30">&nbsp;</td>
  </tr>
  <tr>
    <td>Doc 1</td>
    <td>&pound;15</td>
    <td><input type="checkbox" name="ppres[doc1]"   value="15.00"  
       
    <?    if (( $_POST["check"] == 'send') && ($checkboxcount > 0)){
	
      	$search_array = ($_POST['ppres']);
			if (array_key_exists('doc1', $search_array)) {
			echo "checked='checked' ";
			}
		}
	?>
     /></td>
  </tr>
  <tr>
    <td>doc2</td>
    <td>&pound;15</td>
    <td><input type="checkbox" name="ppres[doc2]"   value="15.00"        
 <?    if (( $_POST["check"] == 'send') && ($checkboxcount > 0)){
	
      	$search_array = ($_POST['ppres']);
			if (array_key_exists('doc2', $search_array)) {
			echo "checked='checked' ";
			}
		}
	?>
     /></td>
  </tr>
<tr>
    <td>doc3</td>
    <td>&pound;15</td>
    <td><input type="checkbox" name="ppres[doc3]"   value="15.00"   
  <?    if (( $_POST["check"] == 'send') && ($checkboxcount > 0)){
	
      	$search_array = ($_POST['ppres']);
			if (array_key_exists('doc3', $search_array)) {
			echo "checked='checked' ";
			}
		}
	?>
     /></td>
  </tr>
</table>
<h1 style="margin-bottom:0;">Subscription Service</h1>
<div>
<table width="100%" border="0"  cellpadding="4" cellspacing="2"  class="pandp" bgcolor="#FFFF00">
<tr> 
<td colspan="2">Annual subscription for just <strong>£49.99 per year</strong>.<br /><br />
</td>
</tr>
<tr> 
 <td class="contact">Yes please, subscribe me to this service:</td>
 <td><input type="radio" style="border:0px;" name="subscription" value="yes" <? if ( $subscription == 'yes'){ echo "CHECKED"; }?>/></td>
</tr>
<tr> 
  <td class="contact">No thankyou: </td>
   <td><input type="radio" style="border:0px;" name="subscription" value="no" <? if ( $subscription == 'no'){ echo "CHECKED"; }?>/></td>
</tr>
</table>

</div>
<br /><br />
  <input style="margin-left:10px;" type="submit" name="add" value="Proceed"> 
  <input type="hidden" name="check" value="send" />
</form>



The above is fine. However when i prepare the items for the payment processor im running into difficulties.


The script below counts and prepares which "docs" the user wants to purchase ready for the payment processor, in this instance paypal...



Code:
<form name="pay" action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="business" value="thecode">
<input type="hidden" name="country" value="GB">
<input type="hidden" name="invoice" value="<?= $invoice  ?>">
<input type="hidden" name="first_name" value="<?= $name ?>">
<input type="hidden" name="address1" value="<?= $add1 ?>">
<input type="hidden" name="address2" value="<?= $add2 ?>">
<input type="hidden" name="city" value="<?= $add3 ?>">
<input type="hidden" name="zip" value="<?= $pcode ?>">
<input type="hidden" name="night_phone_b" value="<?= $let  ?>">
<input type="hidden" name="email" value="<?= $laim1 ?>">


	<?
	
	$count = 1;
foreach ($_POST['ppres'] as $k => $v) {
    
    echo "
    <input type='hidden' name='item_name_{$count}' value='$k'>
    <!--<input type='hidden' name='item_number_{$count}' value='{$count}'>-->
    <input type='hidden' name='quantity_{$count}' value='1'>
    <input type='hidden' name='amount_{$count}' value='15.00'>";
   $count++;
 } 

	
	 

 ?>
	<input type="hidden" name="return" value="[URL unfurl="true"]http://mysite/success.php">[/URL]
	<input type="hidden" name="cancel_return" value="[URL unfurl="true"]http://mysite/failure.php">[/URL]
    
 
    
    <input style="margin-left:10px; margin-top:10px;" type="submit" name="pay" value="Proceed to Payment Pages"> 

    
    </form>


My question is.. How can I add the subscription service so it becomes one of the items in the paypal cart?

Many thanks if anyone can help

blx
 
ok, there was a bit of scrip missing from the first piece of code in my post



<h1 style="margin-bottom:0;">Subscription Service</h1>
<div>
<table width="100%" border="0" cellpadding="4" cellspacing="2" class="pandp" bgcolor="#FFFF00">
<tr>
<td colspan="2">Annual subscription for just £49.99 per year.<br /><br />
</td>
</tr>
<tr>
<td class="contact">Yes please, subscribe me to this service:</td>
<td><input type="radio" style="border:0px;" name="subscription" value="yes" <? if ( $subscription == 'yes'){ echo "CHECKED"; }?>/></td>
</tr>
<tr>
<td class="contact">No thankyou: </td>
<td><input type="radio" style="border:0px;" name="subscription" value="no" <? if ( $subscription == 'no'){ echo "CHECKED"; }?>/></td>
</tr>
</table>
</div>
<br /><br />
<input style="margin-left:10px;" type="submit" name="add" value="Proceed">
<input type="hidden" name="check" value="send" />
</form>




thanks

blx
 
Just to add a little more detail and to try and simplify what im asking.

Basically the following works fine as it counts through the items that have been selected by the user and displays them in the correct format for paypal.

PHP:
<?
	
	$count = 1;
foreach ($_POST['ppres'] as $k => $v) {
    
    echo "
    <input type='hidden' name='item_name_{$count}' value='$k'>
    <!--<input type='hidden' name='item_number_{$count}' value='{$count}'>-->
    <input type='hidden' name='quantity_{$count}' value='1'>
    <input type='hidden' name='amount_{$count}' value='15.00'>";
   $count++;
 } 

 ?>

What i then need to do is..

If the "subscription" service was selected I need create an additiaonl item to send to paypal.

In paypal each item you are sending has to be sent like this..

<input type='hidden' name='item_name_1' value='doc1'>
<input type='hidden' name='quantity_1' value='1'>
<input type='hidden' name='amount_1' value='15.00'>

then item_name_2 etc etc

So i suppose what i need to do is identfy the last item number then if the user selected the additional "subscription" service add this in the above format which we are sending to paypal.

1. How can i identify what the last item number was?
2. How do i then add the additional item?

Below is how the user selects the subscription item...
Code:
<input type="radio" style="border:0px;" name="subscription" value="yes" <? if ( $subscription == 'yes'){ echo "CHECKED"; }?>/>


thanks if anyone come up with a solution

blx
 
so im still plugging away at this.

I suppose what im trying to do is "loosely" something like this, but under if ($subscription == "yes") below, the number for $count needs to be one more than the last foreach ($_POST['ppres'] number.

Code:
<?
	
	$count = 1;
foreach ($_POST['ppres'] as $k => $v) {
    
    echo "
    <input type='hidden' name='item_name_{$count}' value='$k'>
    <!--<input type='hidden' name='item_number_{$count}' value='{$count}'>-->
    <input type='hidden' name='quantity_{$count}' value='1'>
    <input type='hidden' name='amount_{$count}' value='15.00'>";
		
   $count++;
 } 
 

 if ($subscription == "yes") {
	 
	 echo "
	 <input type='hidden' name='item_name_{$count}' value='Annual Update Subscription Service'>
      <input type='hidden' name='quantity_{$count}' value='1'>
    <input type='hidden' name='amount_{$count}' value='49.99'>";
	 
	 
 	}
?>


Can anyone help?

blx
 
ok, so i've solved my own post. yipeeee

What i needed to do is count the array.

count($_POST['ppres']);

i then have the number i wanted and added 1 if the user wants the subscription.

Lovely.


blx

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top