0

I am using JavaScript within PHP to retain form values. Part of the form is dynamically generated, so based upon how children the user has (it is an insurance form). As such, there are times when, based upon the user's input, certain PHP values simply will not exist ($childBirthyear2 will not exist if the user selected only one child). Obviously this presents a problem with Javascript.

I thougt I would be able to work around it with a conditional statement. This did not end up being the case. I still get the same error, I was before implementing the conditional, specifically:

Timestamp: 7/16/2016 6:53:15 PM
Error: SyntaxError: expected expression, got ')'
Source File: https://insurancemidam.com/test/confirmation.php
Line: 665, Column: 41
Source Code:
  cycleSelectOptions('#childBirthyear3', ); 

Now I understand why it is getting the error (the code, before it is run in the browser, reads cycleSelectOptions('#childBirthyear3', $childBirthyear3 ) and $childBirthyear3 does not exist in this instance); however, I am not quite sure WHY this code is even being reached. To understand what I mean, this is how the code looks to the browser:

cycleSelectOptions('#childBirthyear1', 1900);
cycleSelectOptions('#childBirthday1', 01);
cycleSelectOptions('#childBirthmonth1', 11);


if(2 >= 2) {
  cycleSelectOptions('#childBirthyear2', 1900);
cycleSelectOptions('#childBirthday2', 01);
cycleSelectOptions('#childBirthmonth2', 01);
}

if(2 >= 3) {
  cycleSelectOptions('#childBirthyear3', );
cycleSelectOptions('#childBirthday3', );
cycleSelectOptions('#childBirthmonth3', );
}
 if(2 >= 4) { cycleSelectOptions('#childBirthyear4', );
cycleSelectOptions('#childBirthday4', );
cycleSelectOptions('#childBirthmonth4', );
}

 if(2 >= 5) { cycleSelectOptions('#childBirthyear5', );
cycleSelectOptions('#childBirthday5', );
cycleSelectOptions('#childBirthmonth5', );
}

 if(2 >= 6) { cycleSelectOptions('#childBirthyear6', );
cycleSelectOptions('#childBirthday6', );
cycleSelectOptions('#childBirthmonth6', );
}
 if(2 >= 7) { cycleSelectOptions('#childBirthyear7', );
cycleSelectOptions('#childBirthday7', );
cycleSelectOptions('#childBirthmonth7', );
}
 if(2 >= 8) { cycleSelectOptions('#childBirthyear8', );
cycleSelectOptions('#childBirthday8', );
cycleSelectOptions('#childBirthmonth8', ); 

This is the original PHP

if($hasChildren) {
echo" 
if($childBirthyear1) {
  cycleSelectOptions('#childBirthyear1', $childBirthyear1);
cycleSelectOptions('#childBirthday1', $childBirthday1);
cycleSelectOptions('#childBirthmonth1', $childBirtmonth1);
}

if($childBirthyear2) {
  cycleSelectOptions('#childBirthyear2', $childBirthyear2);
cycleSelectOptions('#childBirthday2', $childBirthday2);
cycleSelectOptions('#childBirthmonth2', $childBirtmonth2);
}

if($childBirthyear3) {
  cycleSelectOptions('#childBirthyear3', $childBirthyear3);
cycleSelectOptions('#childBirthday3', $childBirthday3);
cycleSelectOptions('#childBirthmonth3', $childBirtmonth3);
}
 if($childBirthyear4) { cycleSelectOptions('#childBirthyear4', $childBirthyear4);
cycleSelectOptions('#childBirthday4', $childBirthday4);
cycleSelectOptions('#childBirthmonth4', $childBirtmonth4);
}

 if($childbirthyear5) { cycleSelectOptions('#childBirthyear5', $childBirthyear5);
cycleSelectOptions('#childBirthday5', $childBirthday5);
cycleSelectOptions('#childBirthmonth5', $childBirtmonth5);
}

 if($childBirthyear6) { cycleSelectOptions('#childBirthyear6', $childBirthyear6);
cycleSelectOptions('#childBirthday6', $childBirthday6);
cycleSelectOptions('#childBirthmonth6', $childBirtmonth6);
}
 if($childBirthyear7) { cycleSelectOptions('#childBirthyear7', $childBirthyear7);
cycleSelectOptions('#childBirthday7', $childBirthday7);
cycleSelectOptions('#childBirthmonth7', $childBirtmonth7);
}
 if($childBirthYear8) { cycleSelectOptions('#childBirthyear8', $childBirthyear8);
cycleSelectOptions('#childBirthday8', $childBirthday8);
cycleSelectOptions('#childBirthmonth8', $childBirtmonth8);";
}

I did not think the problematic code would be executed - 2 is not >= 3, after all.

Thanks for any help!

3
  • 1
    Arrays and for loops would be so helpful here. What happens if someone has 9 children? :-) Without seeing the rest of the code, it's hard to help much, but I'd say the quick fix is to just assign all those variables values no matter what. E.g. $childBirthyear5 = "null"; above wherever you assign them real values based on the number of actual children. Commented Jul 17, 2016 at 0:11
  • 1
    "I did not think the problematic code would be executed - 2 is not >= 3, after all." - Before the browser executes the JS it first parses it, so syntax errors like the above stop it running at all. Rather than using a single echo to dump the whole block of JS, do the if tests in your PHP and only echo out the cycleSelectOptions() statements that are required. (Also, I don't see how the PHP shown could possibly produce that output to the browser, because you've got the $childBirthyear2 variable somehow outputting 2 >= 2 in the if and 1900 in the next line.) Commented Jul 17, 2016 at 0:13
  • Thanks everyone for all the great answers and advice. I appreciate it. Commented Jul 17, 2016 at 0:57

1 Answer 1

1

I think you'll need to set some value for those variables in PHP before you try to use them in the string of JavaScript. Maybe something simple like:

if($hasChildren) {
    echo "
    ...
    cycleSelectOptions('#childBirthyear3', " . ((isset($childBirthyear3))?$childBirthyear3:"''") . ");

    ...";
}

That says if the PHP variable is not set, then insert a blank string into the JS... of course, you could replace that "default" value with whatever you like.

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

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.