Forum

Thread tagged as: Question, Problem, PayPal

Paypal shop array in jQuery but not in PHP

I am using Paypal Shop and adding on my own cart session and have an issue that is baffling me. I will eventually be using jQuery and Ajax to update the shopping cart but I also have the 'fallback' of a direct form post should a user not have JavaScript enabled.

So, to test the data submitted by the form for a product (for which there are multiple option choices) I initially did a console log for the posted data and used that to determine how I created the session arrays for my cart. However, things didn't tally as I thought and the easiest way to describe the difference is to show the outputs.

optionID: Array[5]
0: "1"
1: "2"
2: "3"
3: "4"
4: "6"
length: 5
__proto__: Array[0]
option_1: "Black"
option_2: "Green"
option_3: "Contactacoat"
option_4: "No Slats"
option_6: "3 foot"

The above is what is output in the console so I expected an an optionID array within the posted data, but when I print out the posted data directly with PHP, I get the following

[optionID] => 6
    [option_1] => Black
    [option_2] => Green
    [option_3] => Longfield
    [option_4] => No Slats
    [option_6] => 3 foot

So as you can see the optionID is no longer an array but just a key with the value of the last option id.

Any idea why this is?

Emma Davis

Emma Davis 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Could you give some context? Where is this data coming from?

The data is coming from a form on the product details page. Is that what you mean?

<perch:if exists="productOptions_html">
    <input type="hidden" name="has_options" value="1">
    <perch:shop id="productOptions_html" encode="false" type="hidden" />
</perch:if>

OK, I think I know what is wrong now. As I have added in the functionality to have more than one option choice I needed to change the name of the form field from name="optionID" to name="optionID[]" to create an array of the option id's for the php.

It appears that jQuery/Ajax is automatically recognising the multiple form fields with the same name as an array (or at least creating an array from the form fields). Unfortunately putting in the array brackets is now just amending the name of the object/array within my jQuery but I am sure I can get round that.