Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 1 to a javaScript variable?

Tags:

javascript

php

I want to add 1 to the value "$arate_num" and update the database with new $arate_num called "num3". But it is not working 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);
like image 608
Amare Avatar asked Jun 01 '26 21:06

Amare


1 Answers

Replace:

var num3 = parseInt(phpvalue+1, 10);

With:

var num3 = parseInt(phpvalue, 10) + 1;

phpvalue is a string. Adding 1 to it concatenates the 1 at the end of the string.

Say, phpvalue is "1337", parseInt(phpvalue+1, 10) will be 13371, because you're parsing "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'd number:

var num = phpvalue;
var num3 = phpvalue + 1;
like image 54
Cerbrus Avatar answered Jun 04 '26 12:06

Cerbrus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!