简体   繁体   English

Javascript-在错误条件下运行代码

[英]Javascript - Running Code in False Conditional

I am using JavaScript within PHP to retain form values. 我在PHP中使用JavaScript来保留表单值。 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). 因此,有时根据用户的输入,某些PHP值将根本不存在(如果用户仅选择一个孩子,则$ childBirthyear2将不存在)。 Obviously this presents a problem with Javascript. 显然,这带来了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); 现在,我明白了为什么会收到错误(在浏览器中运行该代码之前,该代码读取cycleSelectOptions('#childBirthyear3', $childBirthyear3 ) ,而在该实例中不存在$ childBirthyear3); 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 这是原始的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. 我不认为有问题的代码将被执行-毕竟2不是> = 3。

Thanks for any help! 谢谢你的帮助!

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. 我认为您需要先在PHP中为这些变量设置一些值,然后再尝试在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. 这就是说,如果未设置PHP变量,则在JS中插入一个空白字符串...当然,您可以将“默认”值替换为所需的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM