Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript not receiving or modifying a value

Tags:

javascript

php

I have a MySQL table that is being displayed on a webpage. I have a button for each row that toggles a function using a value from the row :

<button id="loginButtonSmall" onclick="resumeApplication(<?php echo $row['newApplicationNumb']; ?>)">
 Pickup where we left off
</button>

Value of $row['newApplicationNumb'] displays correctly further up on the page.

Here $row['newApplicationNumb'] = 359786918929342363.

Then I have :

function resumeApplication(applicationID) {
  var newApplicationNumb = applicationID;
  alert(newApplicationNumb); 
}

The alert returns : 359786918929342340.

So it changes the last 2 digits from 63 to 40 each time...

I've looked around and really don't understand how it could change the value like this...

Any clues would be very much appreciated!

like image 817
danny26b Avatar asked Mar 07 '23 12:03

danny26b


2 Answers

See this answer: What is JavaScript's highest integer value that a number can go to without losing precision?

The short answer is that your number is too big and the reliability is lost. I'd use a string value instead.

like image 159
AaronHolland Avatar answered Mar 15 '23 20:03

AaronHolland


The highest integer value in javascript is +/-9007199254740991. If you really need to store higher values use library BigInteger.

The second option is not to pass the parameter as an integer but as a string. Replace this:

<button id="loginButtonSmall" onclick="resumeApplication(<?php echo $row['newApplicationNumb']; ?>)">
 Pickup where we left off
</button>

With this:

<button id="loginButtonSmall" onclick='resumeApplication("<?=$row['newApplicationNumb']?>")'> // Added quotation marks
 Pickup where we left off
</button>
like image 44
mozkomor05 Avatar answered Mar 15 '23 21:03

mozkomor05