Refreshing calculation field when using PHP/JS

Hi everyone I’m running into some issues when combining both the built-in calculation functions in the form along with my own logic that utilizes different numbers in each field depending on dropdown choices that a user makes.

When the form is submitted, the number is reflected accurately and everything is correct, but while the user is filling out the form, my javascript numbers aren’t being reflected in gravity’s calculation.

So for my question: Is there anyway to refresh the calculation field?

You might want to share some of your JS to see if someone could spot any issues with it.

1 Like

Here is the code that I’m using for my page.

add_action('wp_footer','gf_custom_script');
function gf_custom_script(){ //this is for commission disbursement request REAL ESTATE
	?>
	<script type="text/javascript">
		jQuery('document').ready(function(){
			jQuery('#input_6_5, #input_6_17, #input_6_178').change(function(){
				//input_6_5 is sale price 
                //input_6_17 is property type 
				var sale_price = jQuery('#input_6_5').val();
				var property_type = jQuery('#input_6_17').val();
				var insurance_fee = jQuery('#input_6_77').val(); //E&O Insurance/Risk Management Fee
				var smart_buy_combo = jQuery('#input_6_178').val(); //smart-buy combo choice from user 
				var regex = /\d+(?:\.\d+)?/g;
				var match;	
				var matchArray="";
				//var match = regex.exec(sale_price);
                //the exec method executes a search for a match in a specified string and returns a result array or null
				while (match = regex.exec(sale_price)) {
					matchArray+=match[0];
				  
				}
				//var formated_string = match.join('-');
				//pricing = formated_string.replace("-", ""); 
				var broker_commission = calculate_broker_commission(matchArray,property_type);
				var eo_insurance = calculate_eo_insurance(property_type);
				var smart_buy = calculate_smart_buy(smart_buy_combo);
				jQuery('#input_6_179').val(smart_buy); //for smart-buy calculation
				jQuery('#input_6_177').val("+$"+smart_buy); // for smart-buy front-end
				jQuery('#input_6_77').val(-eo_insurance); //for calculation
				jQuery('#input_6_175').val("-$"+eo_insurance); //separate field for visibility
				jQuery('#input_6_141').val("-$"+broker_commission); //this is also company dollar 
				jQuery('#input_6_24').val(-broker_commission); //this is company dollar (hidden) used for calculation
				
				
			});	
                         
			function calculate_broker_commission(sale_price,property_type){
				if(property_type == 'Residential 1-4 Units'){
					if(sale_price >= 0 &&  sale_price <= 1000000 ){
						return 495;	
					}else{
						var sale_price_cal = sale_price/1000000;
						var cal = sale_price_cal.toString();

						if(cal.indexOf('.') !== -1){
							price_array = cal.split('.');
							var divisible = parseInt(price_array[0]) + 1;
							return 495*divisible;
						}else{
							return 495*sale_price_cal;	
						}
					}
				}else if(property_type == 'Residential 1-4 (Smart-Buy Referral)'){
                        return 195;
                }else{
					b_com = jQuery('#input_6_18').val(); //broker net commission $
					var regex = /\d+(?:\.\d+)?/g;
					var match;			
					var b_comArray = "";
					while (match = regex.exec(b_com)) {
						b_comArray+=match[0];
					  
					}	
					return b_comArray*0.2;
				}
			}

			function calculate_eo_insurance(property_type) {
    			if (property_type == 'Residential 1-4 Units') {
        			return 170
    			}else if (property_type == 'Residential 1-4 Units (Smart-Buy Referral)') {
        			return 170;
    			}else if(property_type == 'Residential 1-4 Units Lease'){
					return 170;
				}else {
        			return 270;
    			}
			}

			function calculate_smart_buy(smartbuy){
				if(smartbuy == 'Yes'){
					return 300;
				}else{
					return 0;
				}
			}

		});
	</script>
	<?php
}

Sorry for taking so long to respond. It appears since your jQuery is inside your PHP function that the jQuery will only work once the form has been sent back to your server.

You might want to try adding your jQuery inside an HTML field in your form to see if you can interact with the DOM directly instead of waiting for the form data to be sent to the database.

1 Like

Hi Derek,
Thanks for getting back to me. I’m still new to web development with wordpress and maybe I’m overthinking it, but how should I go about adding jQuery to an HTML field?

What you said about the DOM makes complete sense, I just don’t know where to put the code now.

You should just be able to wrap opening and closing script

<script> // jQuery goes inside script tags and minus the //</script>

tags around your jQuery and then add it to your HTML field. Be sure to make your HTML field hidden as well.

Be default jQuery is loaded on all pages so this should be sufficient.

Let us know if that works.

I can help out more though if you give that a try and share the results.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.