Click here to Skip to main content
15,512,502 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I try to input decimal number which is 44.5, but it return 44.0. How do I get correct decimal values when user input?

What I have tried:

<label>Insert value :</label>
<a class="bold scoreClick">Click to change score</a>																			<div class="bold scoreInputdiv" style="display:none;">
<input class="bold scoreInput" type="number" step="any" size="2" id="score_final_a" value="'.$data['final_score_sectiona'].'"/> /50 
<button type="button" class="btn btn-success btnFinalASave">Save</button></div>


$('.scoreClick').on("click", function (e){
	e.preventDefault();
	var tt = $(this).next('.scoreInputdiv');
	tt.show();
	$(this).hide();
	tt.children('.scoreInput').focus().select();
});
//FINAL
	$('.btnFinalASave').on("click", function (e){
	e.preventDefault();
	var score = $(this);
	var a = parseFloat($(score).prev('.scoreInput').val().trim()) || 0;
	var assessment_performance_id = <?php echo $data['id'];?>;
	var score_final_b = $('#score_final_b').val();
	var score_final_c = $('#score_final_c').val();
			
	if (a > 0){
		$(score).prev('.scoreInput').val(a.toString());
	}
	else{
		$(score).prev('.scoreInput').val('');
	}
			
	$.ajax({
		type: 'GET',
		url: './ajax/update_final_score_assessment.php',
		data: {
			'assessment_performance_id' : assessment_performance_id,
			'score_final_a' : a.toString(),
			'score_final_b' : score_final_b,
			'score_final_c' : score_final_c
		},
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			beforeSend: function(){
					
		},
		    success: function(data){},
			error: function(message){
				//response([]);
				//console.log(message);
			},
			complete: function(){
				if (a > 0){	
						 
    $(score).parent('.scoreInputdiv').prev('a').html(a.toString());
			}
	else{
	   $(score).parent('.scoreInputdiv').prev('a').html('-');
	}
					
	$(score).parent('.scoreInputdiv').prev('a').show();
	$(score).parent('.scoreInputdiv').hide();
	}
 });
});


My ajax code
PHP
<pre><?php
$data = false;
if(isset($_GET['assessment_performance_id'])) {
	include_once '../config/general.php';
	include_once '../config/db.php';
	include_once '../config/myApp.php';
	
	$assessment_performance_id = (int)$_GET['assessment_performance_id'];
	//$score_final_a = (int)$_GET['score_final_a'];
	//$score_final_b = (int)$_GET['score_final_b'];
	//$score_final_c = (int)$_GET['score_final_c'];
	
	$score_final_a = 0;
	if (isset($_GET['score_final_a'])){
		$score_final_a = (int)$_GET['score_final_a'];
	}

	$score_final_b = 0;
	if (isset($_GET['score_final_b'])){
		$score_final_b = (int)$_GET['score_final_b'];
	}

	$score_final_c = 0;
	if (isset($_GET['score_final_c'])){
		$score_final_c = (int)$_GET['score_final_c'];
	}
	
	//$final_overall_score = 0;
	$final_overall_score = $score_final_a + $score_final_b + $score_final_c;
	
	$validProcess = true;
	
	if ($validProcess){
		$data = true;

		if ($score_final_a){

			$mySQL = 'UPDATE assessment_performance SET 
			final_score_sectiona = "'.number_format((float)$score_final_a,1,'.','').'",
			final_overall_score = "'.number_format((float)$final_overall_score,1,'.','').'"
			WHERE id = '.$assessment_performance_id;
			mysql_query($mySQL);
		}
		if ($score_final_b){

			$mySQL = 'UPDATE assessment_performance SET 
			final_score_sectionb = "'.number_format((float)$score_final_b,1,'.','').'",
			final_overall_score = "'.number_format((float)$final_overall_score,1,'.','').'"
			WHERE id = '.$assessment_performance_id;
			mysql_query($mySQL);
		}

		if ($score_final_c){

			$mySQL = 'UPDATE assessment_performance SET 
			final_score_sectionc = "'.number_format((float)$score_final_c,1,'.','').'",
			final_overall_score = "'.number_format((float)$final_overall_score,1,'.','').'"
			WHERE id = '.$assessment_performance_id;
			mysql_query($mySQL);
		}
			
	}

}

header("Content-type: text/x-json");
print json_encode($data);
?>
Posted
Updated 1-Sep-22 16:07pm
v3
Comments
Sandeep Mewara 31-Aug-22 23:20pm    
I would debug JS to see where the value is getting trimmed.

A simple input of numeric type when evaluated for val() in JS will give entire value and not trim or floor it.
Zatil Syamimi 1-Sep-22 21:16pm    
Ouh, I try the code but it work. Did you have any other solutions or best practices ? I'm a beginner and always want some guidance from the senior or experts. ;)
Richard Deeming 1-Sep-22 4:04am    
The HTML you've posted doesn't match the Javascript. Your script is looking for an element with class="scoreInput", but your HTML input only has class="bold".

Post the actual markup of the element you're trying to access.
Richard MacCutchan 1-Sep-22 5:44am    
As a non-expert, does the use of size="2" affect what is captured?
Richard Deeming 1-Sep-22 6:25am    
No, that just defines how big the input will be.

The step attribute would affect the validation, but also shouldn't affect the value returned.
HTML attribute: step - HTML: HyperText Markup Language | MDN[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900