PDA

View Full Version : a little help w/js



12Strings
01-28-2015, 10:41 PM
Hi, In this calculator clicking statement #1 produces the result.
clicking statement #2 submits the form. I'd like for statement #1
to do both but haven't been able to do so??


1) <td><input type="button" name="equal" class="red" value=" = " onClick="EqualButton(); return true;"></td>

2) <input type="submit" name="keypad" id="keypad" value="submit">

</form>

jscheuer1
01-29-2015, 01:46 AM
It could be more complicated (like if the EqualButton function is asynchronous in some way, etc.) But in a fairly straightforward setup (addition red):


<td><input type="button" name="equal" class="red" value=" = " onClick="EqualButton(); this.form.submit(); return true;"></td>

The page made need to be refreshed, and/or the browser cache emptied to see changes. If you want more help - please include a link to the page on your site that has the problematic code.

12Strings
01-29-2015, 10:46 PM
thanks I'll try it It's localhost

12Strings
02-01-2015, 08:21 PM
I'm at a loss (again) and wouldn't pass up some help.

below is the JS function w/variables (verified) that I'm trying to
send to PHP.


<FORM name="calculator" id="calculator"
action="http://localhost/home/webdev_insert.php" method="post">
...................................


function OnCalc(purpose,value1,op,value2,total)
{return(purpose,value1,op,value2,total);}

the message below tells me the variables didn't get passed:


Notice: Undefined index: data in C:\xampp\htdocs\home\webdev_insert.php on line 24
No data submitted for 'purpose'!
-------------------------------------

<?php
function _NewDB()
{
try
{
$databaseName = "homedb";
$databaseUser = "root";
$databasePass = "cookie";
$dbh = new PDO('mysql:host=localhost;dbname='.$databaseName, $databaseUser, $databasePass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{ error_log("PDOException: " . $e); return false; }
return $dbh;
}
// End connection stuff

$dbh = _NewDB();
$fieldList = array("purpose", "value1", "op", "value2", "total");

if(!$_POST) exit("No data submitted via POST!");

// ****************** line 24 below ******************
$data = json_decode($_POST['data'], true);
// ****************************************************

foreach($fieldList as $field) if(!$data[$field]) exit("No data submitted for '" . $field . "'!");

$stmt = $dbh->prepare
("INSERT INTO calculator ( purpose, value1, op, value2, total) VALUES(:purpose, :value1, :op, :value2, :total)");
$stmt->execute($data);
echo 'new row inserted';

// Below I'm pulling rows from the table
$stmt = $dbh->prepare("SELECT * FROM calculator");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

12Strings
02-09-2015, 05:45 AM
ok, sending form data to PHP via ajax but printout shows zero values. My JS editor indicates that one of the scripts below (commented) is invalid. I don't see it. help?


<!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 form</title>
<style type="text/css">
input{
text-align:center;
}
</style>
</head>
<body><center>
<input type="text" size="45" align="middle" value="enter purpose

in box below" ><br>
<input type="text" align="middle" name="purpose" size="45" >
<FORM name="Keypad"

action="http://localhost/home/calcinsert.php" 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 type="button" name="btnEquals" class="red" value=" =

" onClick="Operation('='); this.form.submit();
event.returnValue = false;"></td>

</TR></TABLE></TABLE></FORM>

<a href="http://localhost/home/calcprint.php">Print</a>
</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>

// ************error indicated in below script********

<script>
var js_var = "<br />whatever";
document.getElementById("link").onclick = OnCalc ();
{
// ajax start
var xhr;
if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
else xhr = new ActiveXObject("Microsoft.XMLHTTP"); // for IE
var url = 'http://localhost/home/calcinsert.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>


<?php
$servername = "localhost";$username = "root";$password = "cookie";
$dbname = "homedb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{ die("Connection failed: " . mysqli_connect_error()); }
$sql = "INSERT INTO calculator (purpose, value1, op, value2, total)
VALUES ('purpose', 'value1', 'op', 'value2', 'total')";
if (mysqli_query($conn, $sql))
{
echo "<center>";
echo "<b>data entered<br><br>";
}
else
{ echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
mysqli_close($conn);

header( "refresh:3;url='http://localhost/home/calcform.html'");
echo "<center>";
echo 'I\m old and slow</br>
in a rush, click
<a href="http://localhost/home/calcform.html">here</a>.'
?>