0

I've got a form that collects passenger's details.

The fields it collects are :

passenger-firstname 
passenger-surname   
passenger-address1  
passenger-address2  
passenger-town
passenger-passportnumber

After each passenger I am adding a number (Dependant on what's selected)

So the items would then look like :

passenger1-firstname    
passenger1-surname  
passenger1-address1 
passenger1-address2 
passenger1-town
passenger1-passportnumber

passenger2-firstname    
passenger2-surname  
passenger2-address1 
passenger2-address2 
passenger2-town
passenger2-passportnumber

This number changes dependant on how many have been selected

What I'd like to do (On form submit) is to loop through passenger variables and send them to an AJAX call, Which in turn I will store in a PHP session.

var num_travellers = $('.num_travellers').val();

// For loop through travellers
for (i = 0; i < num_travellers; i++)
{
   var passenger_firstname[i] = $('.passenger[i]-firstname').val();
   alert(passenger_firstname);
}

However, When I do this, I get the error message

SyntaxError: missing ; before statement Source File: var passenger[i];

How can I fix this issue and in turn submit it as AJAX? I'm open to changing it to an array or anything of that sort.

Thanks

1
  • First of all, you need to check some guides on JS syntax. Then remove var passenger[i];. Commented Jan 5, 2016 at 15:34

2 Answers 2

1

You can't declare an array like this:

var passenger[i];

Either declare a single variable:

var passenger;

Or declare an array:

var passengers = [];

Maybe you meant to declare the array outside the loop and push to it in the loop? Something like this?:

var passengers = [];
for (i = 0; i < num_travellers; i++)
{
    var passenger;
    alert(passenger); // doesn't make sense, it's undefined at this time

    // set the value to something here?  perhaps this?:
    passenger = {
        firstname: 'some value',
        surname: 'another value',
        etc: 'more values'
    };

    passengers.push(passenger);
}

It's not entirely clear what you're trying to accomplish here, but at the very least the syntax isn't correct for creating an array.

Sign up to request clarification or add additional context in comments.

3 Comments

Cheers @David to clarify, I am using "passenger1-firstname", "passenger1-surname" this 1 will change to a 2 a 3 etc dependant on how many passengers are selected.
@StuBlackett: I'm not sure how you're handling your data, but I've updated the answer with syntax for creating an object to push on to the array. Hopefully that helps.
Thats great. I'll try handle the data that way.
1

You have to define a variable, then assign the passenger[i] to it.

var vPassenger = passenger[i];
alert(vPassenger)

To create an array and push all passengers to it.

var passengers;
for (i = 0; i < num_travellers; i++)
{
 passengers.push(passenger[i])
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.