HI, I'm worrying with a calendar, trying to send to & print out a list of entries. I must not be doing the "OnCalc " function right (in the Html or Php)? Doing an alert of the values in this following presents the right values but no results. Any help? -------------------- <input name="btnEquals" type="Button" value=" = " onclick="Operation('=')"></TD> </TR></TABLE> <INPUT type="image" src="programmer.gif" alt="submit button"><p> </FORM> <font face="Verdana, Arial, Helvetica" size=2> <SCRIPT LANGUAGE="JavaScript"> var FKeyPad = document.Keypad; var purpose = 0; 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); } 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)} <!-- use ajax - OnCalc function to send data to your database. --> function OnCalc(value1,op,value2,total) alert(value1); { var expression = value1 + op +value2 +'='+ total; alert(expression); } </SCRIPT> </body></html> PHP: ------------------------------------------- include ('gethomedb.php'); // now connected to database if(!empty($_POST["submit"])) { echo '<script type="text/javascript">' , 'OnCalc();' , '</script>'; $id = $_POST['id']; $purpose=$_POST['purpose']; $value1=$_POST['value1']; $op=$_POST['op']; $value2=$_POST['value2']; $total=$_POST['total']; $name = $_POST['id']; if(isset($_POST['id'])) { $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); } } ?> PHP:
Well, for starters you've got some really outdated practices there -- by the time you read this it will likely be 2016 not 1997, lose the stupid malfing FONT tag, the HTML 3.2 style upper case tags, and the long deprecated LANGUAGE attribute which just like FONT has no business on any website written after that time. Your scripting should be hooking the elements NOT the other way around (basically lose all that "onclick" nonsense) and to be frank since most of those "input" are not in fact information to be sent server side and are scripting only elements they should 1) Not be INPUT tags, and 2) not be static in the markup, but generated by the script instead. Some good coding practices wouldn't hurt either -- like if you are declaring a bunch of VAR in a row you don't need to say VAR before each and every blasted one, just make them a comma delimited list. Likewise inline evaluations would be WAY more efficient and easier to follow than all those IF that are assigning different values to the same variable. Much less I'm not aware of ANY browser where there is such a thing as document.Keypad, unless you're blindly trying to access the form by it's name which is ALSO outdated 1997 style nyetscape scripting (Going hand in hand with your outdated <SCRIPT LANGUAGE="JAVASCRIPT"> declaration). You also seem to be doing a parseInt on "total" then accessing it as an object -- which is it as parseFloat on an object is gibberish. Did you mean parseInt(total.value) instead of parseInt(total)? If you want to get the value server-side put it in an input in the form (the ONLY thing by the looks of it that should even be an input!) and submit the form normally -- or serialize it and use AJAX if you don't want a page load. I don't think overall we're seeing enough of your markup, as this does NOT look like code for a calendar, it looks like code for a calculator -- and what you are doing in PHP seems to fail to grasp the difference between when and where server-side and client-side code gets executed.
thanks shadow, I'm working on it. I enjoy your tutorials, I find it incredible to find so much false info online, and many get hostile if their code is questioned. My "calculator" came from "JS free code" or something like . the "document.Keypad" included. Passing the JS "OnCalc" funtion is my effort. following is my issue. I'm not finding resolution online. Parse error: syntax error, unexpected '.', expecting variable (T_VARIABLE) or '$' in C:\xampp\htdocs\home\calculator.php on line 13 <!DOCTYPE html><html> <body><center> <?php // /ajax/calculator.php include ('gethomedb.php'); // now connected to database $id = $_POST['id']; $purpose=$_POST['purpose']; $value1=$_POST['value1']; $op=$_POST['op']; $value2=$_POST['value2']; $total=$_POST['total']; $users = $db->query('SELECT * FROM calculator'); // get some data some // javascript using Jquery // send a get request to /ajax/calculator.html $.get("/ajax/calculator.html", function(OnCalc) { // take the response (json of users), and add it to element(s) with users class in the html dom $(".users").html(users); }); $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); ?>
That looks like you slapped jQuery into the middle of your PHP -- unlike chocolate and peanut-butter those two don't taste good together. I really don't think you are grasping what PHP is, what JavaScript is, and how there's a MASSIVE divide betwixt the two. You can't just mix them willy-nilly that way as PHP runs on the server LONG before JS even exists, and JS runs on the browser LONG after PHP is done executing. The two just don't go together the way you're trying to put them together. Can you explain what it is you are ACTUALLY trying to do with this -- again I'd need to see the code for that calculator (not your server-side gibberish that isn't telling me a blasted thing) and then have you explain what it is you are expecting server-side to have ... well, anything to do with. I'm really not grasping what your user input is, or how / what you expect the server to do with it.
-------------------------------------------------------------------------------------------- Ok Shadow, the following code is one of many offered by forum moderators and modified for my project(and tossed). Instead of using a calculator with a paper tape I want to use my computer to enter operands, send the record to a database and when ready, print the "tape" with a grand total. <!DOCTYPE html><html> <body><center> <?php // ajax/javascript using Jquerycalculator.php include ('gethomedb.php'); // now connected to database $id = $_POST['id']; $purpose=$_POST['purpose']; $value1=$_POST['value1']; $op=$_POST['op']; $value2=$_POST['value2']; $total=$_POST['total']; // send a get request to /ajax/calculator.html $users = $db->query('SELECT * FROM calculator'); $.get("/ajax/calculator.html", function(OnCalc) { // take the response (json of users), and add it to element(s) with users class in the html dom $(".users").html(users); }); $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); ?> <a href=calcprint.php">Print</a> </center></body></html> <!DOCTYPE html><html> <!--To POST data like an HTML form, add an HTTP header with setRequestHeader (). Specify the data you want to send in the send() method:--> <head> <meta charset=utf-8> <title>calculator insert</title> <style type="text/css"> input{ text-align:center; } </style> </head> <body><center> <FORM name="Keypad" action="php file" method="post"> <TABLE> <B> <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=" * " 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></TABLE> </B> <input type="submit" name="keypad" value="submit"> </FORM> </CENTER> <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 ) 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> <script type="text/javascript"> var js_var = "<br />what is this?"; document.getElementById("link").onclick = OnCalc () { // ajax start var xhr; if (window.XMLHttpRequest) xhr = new XMLHttpRequest(); // all browsers else xhr = new ActiveXObject("Microsoft.XMLHTTP"); // for IE var url = 'calculator.php?js_var=' + js_var; xhr.open('GET', url, false); xhr.onreadystatechange = function () { if (xhr.readyState===4 && xhr.status===200) {var div = document.getElementById ('update');div.innerHTML=xhr.responseText;} } xhr.send(); // ajax stop return false; } </script> </body></html>
Could you please go back and edit your posts and put [ code ] brackets around the massive amount of code your posting? (just remove the spaces, or use the button in the editor)
<!DOCTYPE html><html> <body><center> <?php // ajax/javascript using Jquerycalculator.php include ('gethomedb.php'); // now connected to database $id = $_POST['id']; $purpose=$_POST['purpose']; $value1=$_POST['value1']; $op=$_POST['op']; $value2=$_POST['value2']; $total=$_POST['total']; // send a get request to /ajax/calculator.html $users = $db->query('SELECT * FROM calculator'); $.get("/ajax/calculator.html", function(OnCalc) { // take the response (json of users), and add it to element(s) with users class in the html dom $(".users").html(users); }); $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); ?> Code (markup): <a href=calcprint.php">Print</a> </center></body></html> Code (markup): <!DOCTYPE html><html> <!--To POST data like an HTML form, add an HTTP header with setRequestHeader (). Specify the data you want to send in the send() method:--> <head> <meta charset=utf-8> <title>calculator insert</title> <style type="text/css"> input{ text-align:center; } </style> </head> <body><center> <FORM name="Keypad" action="php file" method="post"> <TABLE> <B> <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=" * " 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></TABLE> </B> <input type="submit" name="keypad" value="submit"> </FORM> </CENTER> <font face="Verdana, Arial, Helvetica" size=2> <script> 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> <script> var js_var = "<br />what is this?"; document.getElementById("link").onclick = OnCalc () { // ajax start var xhr; if (window.XMLHttpRequest) xhr = new XMLHttpRequest(); // all browsers else xhr = new ActiveXObject("Microsoft.XMLHTTP"); // for IE var url = 'calculator.php?js_var=' + js_var; xhr.open('GET', url, false); xhr.onreadystatechange = function () { if (xhr.readyState===4 && xhr.status===200) {var div = document.getElementById ('update');div.innerHTML=xhr.responseText;} } xhr.send(); // ajax stop return false; } </script> </body></html> Code (markup):
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> Code (markup): ---------------------------------------------- <?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; ?> PHP:
You should really be more clear on what is you are exactly trying to do here. You state in your first post that you are doing something with a calendar, though the word calculator can be found multiple times throughout your coding. Also, if you were in fact trying to make a calculator PHP would be quite unnecessary and using MySQLi/databases would be even more-so unnecessary. That being said it appears you're trying to make a calculator from what I'm reading from your script, it also looks like you are trying to write what the user is doing on the calculator into a database... why are you doing this? Secondly, many online calculators and free codes for calculators already exist. Stop reinventing the wheel, your time would be better spent working on some original project (or at least a project that hasn't been covered so heavily already). Either way, I recommend you go back and brush up on your coding before diving into another coding project.
Pudge, I'm always happy to hear from another rep. ur right , I did originally mistate the project , but as u stated ..., u no the many corrections. All my efforts are as a pasttime. I appreciate your help but why would u ask my reason? Not all are burdened by deadline and worry re. job security. I'm sure that u, like me, enjoy programming or u wouldn't endure students like me lol. As I slooowly grasp advice I change course(and I realize that drives y'all crazy). I am evolving from my days of Fortran & RPG. (ask DeathShadow). I'm playing w/several things. Inquiring of a dozen will advise do or don't - just php, php & javascript, use jquery, mysqli, PDO, object, procedural, etc.,etc. Through it all, my issue is getting the 4 variables to php. I'll try not to bother u further.
You can't really pass variables from JavaScript to PHP. You can "kind of" do it from PHP to JavaScript like so: <?php $variable = "string"; echo "<script type='text/javascript'> var v = '" . $variable . "';</script>"; ?> Code (markup): But vice-versa is not possible without using some sort of intermediate. Side note, if you are doing this to practice your coding skills then by all means go ahead, otherwise find something else to do. I do recommend you do some online reading on both languages or check out a book on both of them before continuing.
If you're trying to make a calculator (or calendar, or blogscript, or or or) create the calculator-part in javascript, and make sure that is working. When that is done, you can start worrying about getting the calculations into PHP / a database. What you would do then is send the values or the answer or both to a PHP script running in the background, which would then send the information to the database. For instance. You don't mix javascript and PHP - you utilise PHP to work with the server-side stuff, and provide the input needed via javascript / AJAX.
I really so appreciate the efforts here but let's get rid of this thread, it's getting ugly. My memories bad but I think I;ve said before, the JS worked b4 I tried to hook it up to PHP. Thanks