Jump to content

PHP/Javascript calculator error


Drugoholic

Recommended Posts

Hey there,

 

I'm facing a problem with a calculator on my site, you can see it here: http://axa.seomena.com/fr/calculez-vos-primes/calculez-vos-primes-assurances-medicales

 

The problem is, when you choose the "number of family" whether you're a family of 1 or a family of 6 and more, (in the next step) if you choose anything but "Class SP" the calculation will work, but if you choose "Class SP" in the "Montant Police(US$)" cell instead of printing a value number it just says "NAN", I donno what's the problem with it but I'm assuming it's something to do with the php or some missing values in phpmyadmin.

 

If you notice in the image attached below that when "Class_type_id" is equal to "5" the "family_type_id" is always equal to 0. class_type_id 5 represent the "Class SP".

 

Here is the code:

ksort($counter);
foreach ($counter as $nb=>$data){

	//echo $age_range_id[$nb].'--'.$class_type_id[$nb].'--'.$nssf[$nb].'--'.$amb[$nb].'--'.$med[$nb].'--'.$doc[$nb].' ||||||| ';
	//echo $nb;
	//if((!$age_range_id[$nb-1])||(!$class_type_id[$nb]))continue;
	if($nssf[$nb]=="on")$add="and nssf=1";
	else $add="and nssf=0";

	if(($amb=="on") && ($age_range_id[$nb-1]))$get_amount_amb=id2fields("amb_plan","amount","family_type_id=$family_type_id and age_range_id =".$age_range_id[$nb-1]."");
	if(($med=="on") && ($age_range_id[$nb-1]))$get_amount_med=id2fields("pre_med_plan","amount","family_type_id=$family_type_id and age_range_id =".$age_range_id[$nb-1]."");
	if(($doc=="on") && ($age_range_id[$nb-1]))$get_amount_doc=id2fields("doc_plan","amount","family_type_id=$family_type_id and age_range_id =".$age_range_id[$nb-1]."");

	//echo $get_amount_amb.'~~~'.$get_amount_med.'~~~'.$get_amount_doc.'<br>';


	if($class_type_id[$nb]<>5){ // NO class SP

		if($age_range_id[$nb-1])$get_amount=id2fields("policy","amount",
			"age_range_id=".$age_range_id[$nb-1]." and class_type_id=".$class_type_id[0]." and family_type_id=$family_type_id $add");

	}
	else { // Class SP

		if($age_range_id[$nb-1])$get_amount=id2fields("policy","amount",
				"age_range_id=".$age_range_id[$nb-1]." and class_type_id=".$class_type_id[0]." $add");

	}
	$sum_amount=$get_amount+$get_amount_amb+$get_amount_med+$get_amount_doc;	
	/*echo "<b>".($get_amount?$get_amount:"NaN").($get_amount_amb?"- $get_amount_amb ":"").($get_amount_med?" - $get_amount_med ":"").($get_amount_doc?" - $get_amount_doc total : ----> $sum_amount ":"")."</b>";
		echo "<br>";*/

//echo "==>".$age_range_id[$nb-1].'<br>';	

if($age_range_id[$nb-1]){		
		echo "<tr>
					<td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".$nb."</td>
					<td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount?number_format($get_amount,2,'.',','):"NAN")."</td>
					<td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount_amb?$get_amount_amb:"  ")."</td>
					<td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount_med?$get_amount_med:"  ")."</td>
					<td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".($get_amount_doc?$get_amount_doc:"  ")."</td>
					<td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'>".number_format($sum_amount,2,'.',',')."</td>
					<input type='hidden' name='amnt[$nb]' value='$get_amount'/>
					<input type='hidden' name='amnt_amb' value='$get_amount_amb'/>
					<input type='hidden' name='amnt_med' value='$get_amount_med'/>
					<input type='hidden' name='amnt_doc' value='$get_amount_doc'/>
					<input type='hidden' name='sum[$nb]' value='$sum_amount'/>
					<input type='hidden' name='nb[$nb]' value='$nb'/>
			</tr>";	

		$total+=$sum_amount;
	}

}
echo "<tr>
			<td align='right' colspan='5' id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'><b>     Total des primes:</b></td>
			<td id='policytd' style='font-size: 10pt; line-height:11pt;  font-family:tahoma;'><b>".number_format($total,2,'.',',')."</b></td>
	 </tr>
	 ";

echo "</table></center><br><br>";

echo '
		<table> 
				<tr><td style="font-size: 8pt; line-height:11pt; font-family:tahoma" colspan="2" id="seperator">Veuillez remplir les informations suivantes pour qu’un de nos agents vous contacte. </td></tr>
				<tr><td style="font-size: 8pt; line-height:11pt; font-family:tahoma">Nom :</td><td align="left"> <input type="text" name="nom"/></td>
				</tr>
				<tr>
					<td style="font-size: 8pt; line-height:11pt; font-family:tahoma">Prénom :</td><td align="left"> <input type="text" name="prenom"/></td>
				</tr>
				<tr>
					<td style="font-size: 8pt; line-height:11pt; font-family:tahoma">No.Cellulaire :</td><td align="left"> <input type="text" name="phone"/></td>
				</tr>
				<tr>
					<td colspan="2"> </td>
				</tr>
				<tr>
					<td style="font-size:12px; font-family:tahoma" width="60%" id="lasttdpadd" colspan="2">
						<input type="submit" value="Soumettre" name="submit"/>
						<input type="hidden" value="true" name="sendmail"/>
						<input type="hidden" value="'.$total.'" name="total"/>
					</td>
				</tr>
			</table><br><br><br><br><br><br><br><br><br><br><br><br><br></form>
			';
exit;

}

if((($_POST["step"]==1)&&($_POST["sbm"]=='Étape suivante'))||($_POST["class_type_id"])){
//printR($_POST);

 

I think the problem is here:

if($class_type_id[$nb]<>5){ // NO class SP

		if($age_range_id[$nb-1])$get_amount=id2fields("policy","amount",
			"age_range_id=".$age_range_id[$nb-1]." and class_type_id=".$class_type_id[0]." and family_type_id=$family_type_id $add");

	}
	/*else { // Class SP

		if($age_range_id[$nb-1])$get_amount=id2fields("policy","amount",
				"age_range_id=".$age_range_id[$nb-1]." and class_type_id=".$class_type_id[0]." and family_type_id=$family_type_id $add");

	}*/
	else { // Class SP

					$get_amount=id2fields("policy","amount",
							"age_range_id=".$age_range_id[$nb]." and class_type_id=".$class_type_id[$nb]." and family_type_id=".$family_type_id[$nb]." $add");

				}

 

As you can see if $class_type_id is not equal to 5, it has a different function and I think the function is wrong. I tried to use 2 different ones with no luck so far.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/170056-phpjavascript-calculator-error/
Share on other sites

Now, i've only dealt with this a few times and its been a while, but even if you see a number as a number, javascript may not. The best way I've found to deal with this, is to multiply it by 1! It seems to work then, but I'm not a js guy.

 


var number = 2; // prolly a get element by


var new_number = number * 1; //////adding this has fixed it for me before...


var answer = number * 5; // calculation


return answer;


 

just my two cents ;)

 

lemme know

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.