How do I add 1 to a javaScript variable?
I want to add 1 to the value "$ arate_num" and update the database with a new $ arate_num named "num3". But it doesn't work properly.
Here is my PHP code.
$jsqla = mysql_query("select * from products where id='$product_id'") or die(mysql_error());
$jfeta = mysql_fetch_assoc($jsqla);
$arate_num = $jfeta['rate_number'];
Here is my javaScript code.
var phpvalue = "<?php echo $arate_num; ?>";
var num = parseInt(phpvalue, 10);
var num3 = parseInt(phpvalue+1, 10);
alert(num3);
source to share
Replace:
var num3 = parseInt(phpvalue+1, 10);
FROM
var num3 = parseInt(phpvalue, 10) + 1;
phpvalue
- line. Adding 1
to it concatenates 1
at the end of the line.
For example, phpvalue
- "1337"
, parseInt(phpvalue+1, 10)
will 13371
, because you are disassembling "13371"
.
However, if you just remove the quotes from:
var phpvalue = "<?php echo $arate_num; ?>";
Like this:
var phpvalue = <?php echo $arate_num; ?>;
Then you can just use the echo number:
var num = phpvalue;
var num3 = phpvalue + 1;
source to share