# Solved: help understanding js code



## kirkwebsites (Sep 10, 2011)

My project is to create records of calculator entries (entry1, operator, 
entry2 and the result). I can't sort it out. 
*








 



































 

[TR]

<TD
[/TD]
*​


----------



## ckphilli (Apr 29, 2006)

What kind of errors are you getting?


----------



## kirkwebsites (Sep 10, 2011)

thanks for the response, no errors just doesn't show the op.


----------



## ckphilli (Apr 29, 2006)

I could be missing it, but I don't see where you are calculating the values or sending the value to the item box. If that is the goal, that's what I would look in to.


----------



## kirkwebsites (Sep 10, 2011)

The HTML 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.


----------



## ckphilli (Apr 29, 2006)

Help me help you a little. Did you find this script somewhere and paste it in? If so, please give me a link.


----------



## kirkwebsites (Sep 10, 2011)

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







 



































 

<TD
[/TD]


----------



## kirkwebsites (Sep 10, 2011)

http://www.htmlfreecodes.com/calculator_in_your_page--view--.htm


----------



## ckphilli (Apr 29, 2006)

Double check your copy/paste. I just copied the code provided from the link into a file and it worked as intended.


----------



## kirkwebsites (Sep 10, 2011)

Correct. I ran the html before proceeding.
Sorry if my query was unclear. What I want to do is to send the values (first entry, operator, second entry and
result) as rows to a database upon pressing '=', II'm understanding that 'Accumulate' is the result, 'operator'
is the operator, 'ReadOut' is the first entry and 'curReadOut' is the second entry. How far off base am I?
I'm sending my code as I tried. 
*I tried several times to get the tags to work!*

<!DOCTYPE html>

calculator insert>

*
<?php
include('calcbegin.html');
error_reporting(E_ALL ^ E_NOTICE); 
// error_reporting(0);
echo "";echo date('m/d/y'); echo "​"; 
$id="''"; 
$con=mysqli_connect("localhost","root","cookie","homedb"); 
// ============== check connection 
if(mysqli_errno($con)) 
{echo "Can't Connect to mySQL:".mysqli_connect_error();} 
else 
{echo "
";} 
if(!empty($_POST["submit"])) 
{ 
$title=$_POST['title'];
$ReadOut=$_POST['ReadOut'];
$operator=$_POST['operator'];
$curReadOut=$_POST['curReadOut'];
$Accumulate=$_POST['Accumulate'];
if(isset($_POST['Accumulate'])) 
{ 
$id = $_POST['id']; 
$name = $_POST['Accumulate']; 
$fetch="SELECT title, ReadOut, operator, curReadOut, Accumulate FROM calculator WHERE Accumulate = '".$name."'"; 
$result = mysqli_query($con,$fetch); 
if(!$result) 
{echo "Error:".(mysqli_error($con));} 
$query = "
INSERT INTO calculator (title, ReadOut, operator, curReadOut, Accumulate)
VALUES ('$title','$ReadOut','$operator','$curReadOut','$Accumulate')";
mysqli_query($con, $query);
mysqli_close($con); 
}
}
?>








 



































 

[TR]

<TD
[/TD]



http://localhost/home/calcprint.php]PRINT<br[/URL]>*​


----------



## ckphilli (Apr 29, 2006)

It's updating Readout and Accumulate as you press the keys. If you enter these lines(just before the closing tag) at the end of the Operation function you will see what's actually happening.


```
console.log(Readout);
console.log(Accumulate);
```
I can't hep you with the php side, but I can tell you I don't think this is going to work without additional variables to store your values.


----------



## kirkwebsites (Sep 10, 2011)

ckphilli said:


> It's updating Readout and Accumulate as you press the keys. If you enter these lines(just before the closing tag) at the end of the Operation function you will see what's actually happening.




```
console.log(Readout);  console.log(Accumulate);
```



> I can't hep you with the php side, but I can tell you I don't think this is going to work without additional variables to store your values


I may not have had the console.log statements in the right place. I think the suggestion of


> I don't think this is going to work without additional variables to store your values."


means like
"Readout=variable1; PendingOp=sign; ReadOut=variable2; Accumulate=total;"?


----------



## ckphilli (Apr 29, 2006)

The way the code is written, you're going to need a counter or something of the sort. 

something like

var counter = 0

every time the function runs, increment the counter...counter++

if counter = 1 readout = first number, if counter = 2 readout2 = second counter.

And so on...


----------



## kirkwebsites (Sep 10, 2011)

Is this what is meant? I'm really not sure the placement of the statements.
var counter = 0;
counter++;
if (counter == 1) {readout = entry1;
if (counter == 2) {PendingOp = sign; readout = entry2; Accumulate=total;}


----------



## ckphilli (Apr 29, 2006)

The readouts have to be different, or else your second will overwrite the first. 

Something like...

var counter = 0;
var readout1 = 0;
var readout2 = 0;
var total = 0;

counter++;

if (counter == 1) {readout1= Readout;}
if (counter == 2) {PendingOp = sign; readout2 = Readout; total=Accumulate}


----------



## kirkwebsites (Sep 10, 2011)

thanks guys I finally caught on!


----------

