Reputation: 2522
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!
Upvotes: 0
Views: 77
Reputation: 13273
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.
Upvotes: 1