Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

help understanding js code


  • Please log in to reply
9 replies to this topic

#1 12Strings

12Strings

  • Members
  • 41 posts
  • OFFLINE
  •  
  • Local time:05:31 PM

Posted 26 September 2014 - 07:56 PM

help understanding js code

My project is to insert into a database table, records of calculator entries (entry1, operator,
 entry2 and the result). I can't sort it out. below is my current code.

 <html><body><center><b> <FORM 
name="Keypad" action="" method="post"> <TABLE border=2 width=50 
height=60 cellpadding=1 cellspacing=5> <TR> <TD 
colspan=5 align=middle> <input type="text" name="item" 
onFocus="this.value=''" 
value="Item"></td> <tr> <TD colspan=3 
align=middle> <input name="ReadOut" type="Text" size=24 value="0" 
width=100%> </TD> <TD </TD> <TD> <input 
name="btnClear" type="Button" value=" C " 
onclick="Clear()"> </TD> <TD><input 
name="btnClearEntry" type="Button" value=" CE " 
onclick="ClearEntry()"> </TD> </TR> <TR> <TD> <input 
name="btnSeven" type="Button" value=" 7 " 
onclick="NumPressed(7)"> </TD> <TD> <input 
name="btnEight" type="Button" value=" 8 " 
onclick="NumPressed(8)"> </TD> <TD> <input 
name="btnNine" type="Button" value=" 9 " 
onclick="NumPressed(9)"> </TD> <TD> </TD> <TD> <input 
name="btnNeg" type="Button" value=" +/- " 
onclick="Neg()"> </TD> <TD> <input 
name="btnPercent" type="Button" value=" % " 
onclick="Percent()"> </TD> </TR> <TR> <TD> <input 
name="btnFour" type="Button" value=" 4 " 
onclick="NumPressed(4)"> </TD> <TD> <input 
name="btnFive" type="Button" value=" 5 " 
onclick="NumPressed(5)"> </TD> <TD> <input 
name="btnSix" type="Button" value=" 6 " 
onclick="NumPressed(6)"> </TD> <TD> </TD> <TD 
align=middle><input name="btnPlus" type="Button" value=" + " 
onclick="Operation('+')"> </TD> <TD 
align=middle><input name="btnMinus" type="Button" value=" - " 
onclick="Operation('-')"> </TD> </TR> <TR> <TD> <input 
name="btnOne" type="Button" value=" 1 " 
onclick="NumPressed(1)"> </TD> <TD> <input 
name="btnTwo" type="Button" value=" 2 " 
onclick="NumPressed(2)"> </TD> <TD> <input 
name="btnThree" type="Button" value=" 3 " 
onclick="NumPressed(3)"> </TD> <TD> </TD> <TD 
align=middle><input name="btnMultiply" type="Button" value=" x " 
onclick="Operation('*')"> </TD> <TD 
align=middle><input name="btnDivide" type="Button" value=" / " 
onclick="Operation('/')"> </TD> </TR> <TR> <TD> <input 
name="btnZero" type="Button" value=" 0 " 
onclick="NumPressed(0)"> </TD> <TD> <input 
name="btnDecimal" type="Button" value=" . " 
onclick="Decimal()"> </TD> <TD 
colspan=3> </TD> <TD> <input 
name="btnEquals" type="Button" value=" = " 
onclick="Operation('=')"> </TD> </TR> </TABLE> <!--</FORM>--> <font 
face="Verdana, Arial, Helvetica" size=2> <SCRIPT 
LANGUAGE="JavaScript"> <!-- Begin var FKeyPad = 
document.Keypad; var Accumulate = 0; var FlagNewNum = 
false; var PendingOp = ""; function NumPressed (Num) 
{ if (FlagNewNum) { FKeyPad.ReadOut.value = 
Num; FlagNewNum = false; } else { if 
(FKeyPad.ReadOut.value == "0") FKeyPad.ReadOut.value = 
Num; else FKeyPad.ReadOut.value += 
Num; } } function Operation (Op) { var 
Readout = FKeyPad.ReadOut.value; if (FlagNewNum && PendingOp != 
"="); else { FlagNewNum = true; if ( '+' == 
PendingOp ) Accumulate += parseFloat(Readout); else if ( '-' 
== PendingOp ) Accumulate -= parseFloat(Readout); else if ( 
'/' == PendingOp ) Accumulate /= parseFloat(Readout); else if 
( '*' == PendingOp ) Accumulate *= 
parseFloat(Readout); else Accumulate = 
parseFloat(Readout); FKeyPad.ReadOut.value = 
Accumulate; PendingOp = Op; } } function 
Decimal () { var curReadOut = FKeyPad.ReadOut.value; if 
(FlagNewNum) { curReadOut = "0."; FlagNewNum = 
false; } else { if (curReadOut.indexOf(".") 
== -1) curReadOut += "."; } FKeyPad.ReadOut.value = 
curReadOut; } function ClearEntry () 
{ FKeyPad.ReadOut.value = "0"; FlagNewNum = 
true; } function Clear () { Accumulate = 
0; PendingOp = ""; ClearEntry(); } function 
Neg () { FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * 
-1; } function Percent () { FKeyPad.ReadOut.value = 
(parseFloat(FKeyPad.ReadOut.value) / 100) * 
parseFloat(Accumulate); } </SCRIPT> <!-- 
Here is result --> <input type="submit" name="??" 
value="CALCULATE"><br><br> </form>

 </body></html>
 
The HTML (below) works, I'm just not able to identify the statements I need, the 2 arguments, the operator and
 the results. I thought I would be able to from the terminology.

I haven't relocated the site of origin but here is the code copied.
 

 <html> <body> <FORM 
name="Keypad"> <TABLE border=2 width=50 height=60 cellpadding=1 
cellspacing=5> <tr> <TD colspan=3 
align=middle> <input name="ReadOut" type="Text" size=24 value="0" 
width=100%> </TD> <TD </TD> <TD> <input 
name="btnClear" type="Button" value=" C " 
onclick="Clear()"> </TD> <TD><input 
name="btnClearEntry" type="Button" value=" CE " 
onclick="ClearEntry()"> </TD> </TR> <TR> <TD> <input 
name="btnSeven" type="Button" value=" 7 " 
onclick="NumPressed(7)"> </TD> <TD> <input 
name="btnEight" type="Button" value=" 8 " 
onclick="NumPressed(8)"> </TD> <TD> <input 
name="btnNine" type="Button" value=" 9 " 
onclick="NumPressed(9)"> </TD> <TD> </TD> <TD> <input 
name="btnNeg" type="Button" value=" +/- " 
onclick="Neg()"> </TD> <TD> <input 
name="btnPercent" type="Button" value=" % " 
onclick="Percent()"> </TD> </TR> <TR> <TD> <input 
name="btnFour" type="Button" value=" 4 " 
onclick="NumPressed(4)"> </TD> <TD> <input 
name="btnFive" type="Button" value=" 5 " 
onclick="NumPressed(5)"> </TD> <TD> <input 
name="btnSix" type="Button" value=" 6 " 
onclick="NumPressed(6)"> </TD> <TD> </TD> <TD 
align=middle><input name="btnPlus" type="Button" value=" + " 
onclick="Operation('+')"> </TD> <TD 
align=middle><input name="btnMinus" type="Button" value=" - " 
onclick="Operation('-')"> </TD> </TR> <TR> <TD> <input 
name="btnOne" type="Button" value=" 1 " 
onclick="NumPressed(1)"> </TD> <TD> <input 
name="btnTwo" type="Button" value=" 2 " 
onclick="NumPressed(2)"> </TD> <TD> <input 
name="btnThree" type="Button" value=" 3 " 
onclick="NumPressed(3)"> </TD> <TD> </TD> <TD 
align=middle><input name="btnMultiply" type="Button" value=" x " 
onclick="Operation('*')"> </TD> <TD 
align=middle><input name="btnDivide" type="Button" value=" / " 
onclick="Operation('/')"> </TD> </TR> <TR> <TD> <input 
name="btnZero" type="Button" value=" 0 " 
onclick="NumPressed(0)"> </TD> <TD> <input 
name="btnDecimal" type="Button" value=" . " 
onclick="Decimal()"> </TD> <TD 
colspan=3> </TD> <TD> <input 
name="btnEquals" type="Button" value=" = " 
onclick="Operation('=')"> </TD> </TR> </TABLE> </FORM> <font 
face="Verdana, Arial, Helvetica" size=2> <SCRIPT 
LANGUAGE="JavaScript"> <!-- Begin var FKeyPad = 
document.Keypad; var Accumulate = 0; var FlagNewNum = 
false; var PendingOp = ""; function NumPressed (Num) 
{ if (FlagNewNum) { FKeyPad.ReadOut.value = 
Num; FlagNewNum = false; } else { if 
(FKeyPad.ReadOut.value == "0") FKeyPad.ReadOut.value = 
Num; else FKeyPad.ReadOut.value += 
Num; } } function Operation (Op) { var 
Readout = FKeyPad.ReadOut.value; if (FlagNewNum && PendingOp != 
"="); else { FlagNewNum = true; if ( '+' == 
PendingOp ) Accumulate += parseFloat(Readout); else if ( '-' 
== PendingOp ) Accumulate -= parseFloat(Readout); else if ( 
'/' == PendingOp ) Accumulate /= parseFloat(Readout); else if 
( '*' == PendingOp ) Accumulate *= 
parseFloat(Readout); else Accumulate = 
parseFloat(Readout); FKeyPad.ReadOut.value = 
Accumulate; PendingOp = Op; } } function 
Decimal () { var curReadOut = FKeyPad.ReadOut.value; if 
(FlagNewNum) { curReadOut = "0."; FlagNewNum = 
false; } else { if (curReadOut.indexOf(".") 
== -1) curReadOut += "."; } FKeyPad.ReadOut.value = 
curReadOut; } function ClearEntry () 
{ FKeyPad.ReadOut.value = "0"; FlagNewNum = 
true; } function Clear () { Accumulate = 
0; PendingOp = ""; ClearEntry(); } function 
Neg () { FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * 
-1; } function Percent () { FKeyPad.ReadOut.value = 
(parseFloat(FKeyPad.ReadOut.value) / 100) * 
parseFloat(Accumulate); } </SCRIPT> </body></html> 


BC AdBot (Login to Remove)

 


m

#2 12Strings

12Strings
  • Topic Starter

  • Members
  • 41 posts
  • OFFLINE
  •  
  • Local time:05:31 PM

Posted 19 October 2014 - 02:58 PM

Nobody knows JS?



#3 Crazy Cat

Crazy Cat

  • Members
  • 808 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Lunatic Asylum
  • Local time:10:31 AM

Posted 07 November 2014 - 05:44 PM

Nobody knows JS?


I think it's more of a case that nobody is willing to assist. Try an actual coding forum?
 

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. ― Albert Einstein ― Insanity is doing the same thing, over and over again, but expecting different results.

 

InternetDefenseLeague-footer-badge.png


#4 12Strings

12Strings
  • Topic Starter

  • Members
  • 41 posts
  • OFFLINE
  •  
  • Local time:05:31 PM

Posted 08 November 2014 - 01:01 AM

thanks, no difference there, only on most forums, I only get sarcasm or " why in hell do you want to that?" or they give a link as if I'm stupid

 

 



#5 Crazy Cat

Crazy Cat

  • Members
  • 808 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Lunatic Asylum
  • Local time:10:31 AM

Posted 08 November 2014 - 01:34 AM

thanks, no difference there, only on most forums, I only get sarcasm or " why in hell do you want to that?" or they give a link as if I'm stupid

I'm sorry that it's become like that?? IF I FIND TIME, I'LL HAVE A LOOK??? No promises!
 

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. ― Albert Einstein ― Insanity is doing the same thing, over and over again, but expecting different results.

 

InternetDefenseLeague-footer-badge.png


#6 Slurppa

Slurppa

  • Malware Study Hall Senior
  • 547 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:12:31 AM

Posted 08 November 2014 - 05:02 AM

Not sure if I understood correctly but you want to get input that was inserted and result of that input. I simply created OnCalc function when operation is inserted and calculated. You can modify OnCalc function to send data to your database. I would suggest using ajax for that.

I didn't have time to bug test this but initial test seemed to work correctly.

 <html> <body> <FORM 
name="Keypad"> <TABLE border=2 width=50 height=60 cellpadding=1 
cellspacing=5> <tr> <TD colspan=3 
align=middle> <input name="ReadOut" type="Text" size=24 value="0" 
width=100%> </TD> <TD </TD> <TD> <input 
name="btnClear" type="Button" value=" C " 
onclick="Clear()"> </TD> <TD><input 
name="btnClearEntry" type="Button" value=" CE " 
onclick="ClearEntry()"> </TD> </TR> <TR> <TD> <input 
name="btnSeven" type="Button" value=" 7 " 
onclick="NumPressed(7)"> </TD> <TD> <input 
name="btnEight" type="Button" value=" 8 " 
onclick="NumPressed(8)"> </TD> <TD> <input 
name="btnNine" type="Button" value=" 9 " 
onclick="NumPressed(9)"> </TD> <TD> </TD> <TD> <input 
name="btnNeg" type="Button" value=" +/- " 
onclick="Neg()"> </TD> <TD> <input 
name="btnPercent" type="Button" value=" % " 
onclick="Percent()"> </TD> </TR> <TR> <TD> <input 
name="btnFour" type="Button" value=" 4 " 
onclick="NumPressed(4)"> </TD> <TD> <input 
name="btnFive" type="Button" value=" 5 " 
onclick="NumPressed(5)"> </TD> <TD> <input 
name="btnSix" type="Button" value=" 6 " 
onclick="NumPressed(6)"> </TD> <TD> </TD> <TD 
align=middle><input name="btnPlus" type="Button" value=" + " 
onclick="Operation('+')"> </TD> <TD 
align=middle><input name="btnMinus" type="Button" value=" - " 
onclick="Operation('-')"> </TD> </TR> <TR> <TD> <input 
name="btnOne" type="Button" value=" 1 " 
onclick="NumPressed(1)"> </TD> <TD> <input 
name="btnTwo" type="Button" value=" 2 " 
onclick="NumPressed(2)"> </TD> <TD> <input 
name="btnThree" type="Button" value=" 3 " 
onclick="NumPressed(3)"> </TD> <TD> </TD> <TD 
align=middle><input name="btnMultiply" type="Button" value=" x " 
onclick="Operation('*')"> </TD> <TD 
align=middle><input name="btnDivide" type="Button" value=" / " 
onclick="Operation('/')"> </TD> </TR> <TR> <TD> <input 
name="btnZero" type="Button" value=" 0 " 
onclick="NumPressed(0)"> </TD> <TD> <input 
name="btnDecimal" type="Button" value=" . " 
onclick="Decimal()"> </TD> <TD 
colspan=3> </TD> <TD> <input 
name="btnEquals" type="Button" value=" = " 
onclick="Operation('=')"> </TD> </TR> </TABLE> </FORM> <font 
face="Verdana, Arial, Helvetica" size=2> <SCRIPT 
LANGUAGE="JavaScript"> 
var FKeyPad = document.Keypad; 
var Accumulate = 0; 
var FlagNewNum = false; 
var PendingOp = ""; 
function NumPressed (Num) 
{ 
if (FlagNewNum) 
{ FKeyPad.ReadOut.value = Num; 
FlagNewNum = false; 
} 
else 
{ 
if (FKeyPad.ReadOut.value == "0") 
FKeyPad.ReadOut.value = Num; 
else 
FKeyPad.ReadOut.value += Num; 
} 
} 
function Operation (Op) 
{ 
var Readout = FKeyPad.ReadOut.value; 
if (FlagNewNum && PendingOp != "="); 
else 
{ 
FlagNewNum = true; 
if ( '+' == PendingOp )
{ 
var temp = Accumulate;
Accumulate += parseFloat(Readout); 
OnCalc(temp,PendingOp,Readout,Accumulate);
}
else if ( '-' == PendingOp ) 
{
var temp = Accumulate;
Accumulate -= parseFloat(Readout); 
OnCalc(temp,PendingOp,Readout,Accumulate);
}
else if ( '/' == PendingOp ) 
{
var temp = Accumulate;
Accumulate /= parseFloat(Readout); 
OnCalc(temp,PendingOp,Readout,Accumulate);
}
else if ( '*' == PendingOp ) 
{
var temp = Accumulate;
Accumulate *= parseFloat(Readout); 
OnCalc(temp,PendingOp,Readout,Accumulate);
}
else 
{
Accumulate = parseFloat(Readout); 
}
FKeyPad.ReadOut.value = Accumulate; 

PendingOp = Op; 
} 
} 
function Decimal () 
{ 
var curReadOut = FKeyPad.ReadOut.value; 
if (FlagNewNum) 
{ 
curReadOut = "0."; 
FlagNewNum = false; 
} 
else 
{ 
if (curReadOut.indexOf(".") == -1) 
curReadOut += "."; 
} 
FKeyPad.ReadOut.value = curReadOut; 
} 
function ClearEntry () 
{ 
FKeyPad.ReadOut.value = "0"; 
FlagNewNum = true; 
} 
function Clear () 
{ 
Accumulate = 0; 
PendingOp = ""; 
ClearEntry(); 
} 
function Neg () 
{ 
FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1; 
} 
function Percent () 
{ 
FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accumulate); 
} 
function OnCalc(value1,op,value2,result)
{
//DO YOUR STUFF HERE
var expression = value1 + op +value2 +'='+ result;
alert(expression);
}
</SCRIPT> 
</body>
</html> 

Member of the Bleeping Computer A.I.I. early response team!


#7 JohnnyJammer

JohnnyJammer

  • Members
  • 1,107 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:QLD Australia
  • Local time:08:31 AM

Posted 12 November 2014 - 10:22 PM

thats a good js calc there Slurppa so 12strings should be happy with that.



#8 12Strings

12Strings
  • Topic Starter

  • Members
  • 41 posts
  • OFFLINE
  •  
  • Local time:05:31 PM

Posted 18 November 2014 - 08:52 PM

thats a good js calc there Slurppa so 12strings should be happy with that.



#9 12Strings

12Strings
  • Topic Starter

  • Members
  • 41 posts
  • OFFLINE
  •  
  • Local time:05:31 PM

Posted 24 December 2014 - 03:09 PM

I need for someone to advise how I'm going wrong with passing my
JS "function(OnCalc()" in the FORM named "calculator.html" to my database:
My form presents as desired but submitting enters 0.00 values.
I just need to know if the following syntax and procedures are correct.
Thanks.
--------------------------------------------------------
 this is from "calculator,html"
 

<FORM name="Keypad" action="http://localhost/home/calc_redirect.php" method="post"> // ??????

if (value1 == parseInt(num))
   {value1.value = parseInt(value1.value)}
else
   {value1.value = parsefloat(value1.value)}

if (value2 == parseInt(num))
   {value2.value = parseInt(value2.value)}
else
   {value2.value = parsefloat(value2.value)}

if (total == parseInt(num))
   {total.value = parseInt(total.value)}
else
   {total.value = parsefloat(total.value)}
 
/* var expression = value1 + op +value2 +'='+ total;
alert(expression); */
 }
function OnCalc(value1,op,value2,total)
{
</SCRIPT>

-------------------------------------------------------

<?php
// name of this is "calc_redirect" 
/* The header() sends a raw HTTP/1.1 specification specific header. header() must be called before any actual output is sent */
/* Redirect browser */
header("Location: http://localhost/home/calculator.php"); // ?????
exit;
?>
--------------------------------------------------------
this is from "calculator.php":
$query = "
INSERT INTO calculator (purpose, value1, op, value2, total)
VALUES ('$purpose','$value1','$op','$value2','$total')";
mysqli_query($con, $query);
mysqli_close($con); 
   ?>
<a href="http://localhost/home/calc_print.php">Print</a>
</center></body></html>

--------------------------------------------------------
 
 




 



#10 12Strings

12Strings
  • Topic Starter

  • Members
  • 41 posts
  • OFFLINE
  •  
  • Local time:05:31 PM

Posted 27 January 2015 - 08:25 PM

this is a localhost project
 I key in the calculaor values "purpose", "value1", "op", "value2" & "total".
1)the user(me) is given the option YES and NO of refreshing the database and The options SuBMIT and PRINT.
2)option YES refeshes the database
3)submitting NO inserts the record into database and returns to the calculator for another entry.
4)clicking PRINT prints the report
Being relatively inexperienced(inept), I don't know how to get the 4 variables to the php. I do know I don't need ajax. I don't get errors but also no insert. The problem is that I don't know how
to forward the variables. I've been referred to dozens of relative sites. trying not to be verbose, I'm trimming the code.
 

 

 

 

<form name="Keypad" action="http://localhost/home/PHPinsert.php" method="post"> <input type="text" size = 50 STYLE="color: #000000; background-color: #ccffff;" name="purpose" value="what's this for?" onFocus="this.value=''"><br> <TD colspan=3> </TD> <TD><input name="btnEquals" type="Button" value="    =    " onclick="Operation('=')"></TD> </TR> </TABLE> <input type="submit" name="keypad" value="submit"> </FORM> <font face="Verdana, Arial, Helvetica" size=2>   <SCRIPT> } /* ****************************************************** function OnCalc(value1,op,value2,total) {return(value1,op,value2,total);} /* ****************************************************** </SCRIPT> <a href="http://localhost/home/calcprint.php">print</a> </center></b></font></body></html>

 

 

----------------------------------------------

<?php
  include ('gethomedb.php');
  // now connected to database 
        if(!empty($_POST["submit"]))
 {
   $purpose=$_POST['purpose'];
     $value1=$_POST['value1'];
             $op=$_POST['op'];
     $value2=$_POST['value2'];
       $total=$_POST['total'];
          
    $fetch="SELECT * FROM calculator";   
        $result = mysqli_query($con,$fetch); 
         if(!$result) 
          {echo "Error:".(mysqli_error($con));}
 // ===========================================================  
 $query = "
 INSERT INTO calculator (purpose, value1, op, value2, total)
 VALUES ('$purpose','$value1','$op','$value2','$total')";
 mysqli_query($con, $query);
 mysqli_close($con);
}
/* Redirect browser */
header("Location: http://localhost/home/PHPinsert.html");
exit;
?>


 

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users