Forums

Looking for a bit of code

InternEd 27 Apr, 2012
Hi,

For a form I am creating, I need to pass an amount to a variable that I can pass on to the Paypal redirect.

I suppose I need to create that variable in a custum code section after the form but before the Paypal action. The amount depends on a selection people make in the form. I have a selectbox named 'product' that looks someting like this:

buy1=Education/Not for Profit 1-100 users
buy2=Education/Not for Profit 100-250 users
buy3=Education/Not for Profit 250-1000 users
buy4=Corporate 1-100 users
etc.

I suppose I need to create some 'if then' code to make this work like IF product=buy1 THEN amount = 100, IF product=buy2 THEN amount = 150 etc.

By searching on the forum I found some code that looks like this. Will this work? Do I have to put some code around it?


var amount= new Array();
amount["buy1"]=100;
amount["buy2"]=150;
amount["buy3"]=200;


Can someone help me with the proper piece of code?
Thanks!
GreyHead 27 Apr, 2012
Hi InternEd,

I suppose I need to create that variable in a custom code section after the form but before the Paypal action.

Yes, exactly that.

The code you found looks like JavaScript rather than PHP, please try this:

<?php
$amount_array = array(
  'buy1' => 100,
  'buy2' => 150,
  'buy3' => 200
);
$product = $form->data['product'];
if ( array_key_exists($product, $amount_array) ) {
  $form->data['amount'] = $amount_array[$product];
} else {
  $form->data['amount'] = 99999;
}
?>
Then use amount in the Redirect URL action.

You probably need to handle the exception case when 'product' isn't set in some other way though.

Bob
InternEd 27 Apr, 2012
Thanks Bob!

You probably need to handle the exception case when 'product' isn't set in some other way though.


I was thinking that since the field 'product' is set to 'required' in the validation tab of the field, it is not possible to be empty?
GreyHead 27 Apr, 2012
Hi InterEd,

Unlikely but not impossible. If you turn JavaScript off in your browser you'll find that you can submit with nothing selected. That's why you can still get spam-bot postings that are empty.

Bob
InternEd 28 Apr, 2012
I'm testing the code, but everytime 99999 is returned into the paypal redirection.

I did check the 'product' variable, and that has the right content like 'buy1' or 'buy2' etc.

Does anyone know what can be wrong?
InternEd 28 Apr, 2012
I did change the code to this:

<?php
    $amount_array = array(
      'buy1' => 100,
      'buy2' => 150,
      'buy3' => 200
    );
    $product = $form->data['product'];
    $form->data['amount'] = $amount_array[$product];
    ?>


And now it works. Do i need the if/else construction?
GreyHead 28 Apr, 2012
Hi InternetEd,

Sorry, my mistake, the if needs to check the array keys not the array values so ti should be
if ( array_key_exists($product, $amount_array) ) {
I changed my earlier post.

You probably don't need it is your form validation tests that there is a value set.

Bob
This topic is locked and no more replies can be posted.