admin管理员组文章数量:1287840
I am calling a javascript function from php using a print statement to print out html code, and I am passing in an integer. However, the value when it is passed in php is not matching the number that the javascript function receives, and I have no clue why.
Here is the php that calls the javascript function:
$upc = $items[$i]->GetUPC();
print "upc: " . $upc . "<br/>";
$delete = "<a href='#' onclick='removeFromCart(". $upc .")' ><img src='".$redx."' height='17' width='17' /></a>";
The print statement prints out the value: 0011110416605
And on the page, using view source, here is the tag for this:
<a href='#' onclick='removeFromCart(0011110416605)' >
Here is the javascript function that is being called:
function removeFromCart(itemID){
var option = document.getElementById("select_cart");
var selected = option.options[option.selectedIndex].value;
alert(itemID);
loadCart(itemID,selected,"","remove");
}
The alert box is printing out the number: 1226972549
I could understand if it was removing the leading 0's, but this number is pletely different. Does anyone know why this may be?
I am calling a javascript function from php using a print statement to print out html code, and I am passing in an integer. However, the value when it is passed in php is not matching the number that the javascript function receives, and I have no clue why.
Here is the php that calls the javascript function:
$upc = $items[$i]->GetUPC();
print "upc: " . $upc . "<br/>";
$delete = "<a href='#' onclick='removeFromCart(". $upc .")' ><img src='".$redx."' height='17' width='17' /></a>";
The print statement prints out the value: 0011110416605
And on the page, using view source, here is the tag for this:
<a href='#' onclick='removeFromCart(0011110416605)' >
Here is the javascript function that is being called:
function removeFromCart(itemID){
var option = document.getElementById("select_cart");
var selected = option.options[option.selectedIndex].value;
alert(itemID);
loadCart(itemID,selected,"","remove");
}
The alert box is printing out the number: 1226972549
I could understand if it was removing the leading 0's, but this number is pletely different. Does anyone know why this may be?
Share Improve this question edited Nov 21, 2013 at 17:53 Vivin Paliath 95.6k42 gold badges230 silver badges301 bronze badges asked Nov 21, 2013 at 17:48 Jason247Jason247 1,0244 gold badges20 silver badges39 bronze badges 1-
1
Put quotes around that.
removeFromCart("0011110416605")
– jszobody Commented Nov 21, 2013 at 17:49
4 Answers
Reset to default 11JavaScript thinks it is an octal value (because of the leading zero and the lack of digits greater than 7
). The decimal value of octal 0011110416605
is 1226972549
. Example:
> var value = 010; //10 in octal
> console.log(value);
> 8 //is 8 in decimal
Use a string instead:
removeFromCart("0011110416605")
Solve it by creating a hidden text called it 'tempcart' then assing value to it with HTML or PHP, it will keep the leading zeros intact as a text string. To make sure, convert it to string if you are passing it thru PHP
HTML:
<input type='hidden' id='tempcart' value='0011110416605'>
PHP:
$itemID = '0011110416605';
$itemID = strval( $itemID );
echo "<input type='hidden' id='tempcart' value=$itemID >";
SCRIPT:
function removeFromCart() {
var itemID = document.getElementById('tempcart').value;
alert(itemID);
//do whatever...
}
This is just sample programming, you need to fit it in your program. I guess Javascript can't pass due to convertion to octal value (because of the leading zero and the lack of digits greater than 7) as @Vivid mention before.
You can just call with including '' quotes
Like onchange="updateLineTotal{$id,'{$po101}')";
Just include it in quotes so it will consider it as string not an integer
I found this issue and I fixed it by using parseInt:
itemID = parseInt('0011110416605',10);
本文标签: phpNumber with leading zeroes gets changed in JavaScriptStack Overflow
版权声明:本文标题:php - Number with leading zeroes gets changed in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741318653a2372074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论