0

Hi, I could use some help passing a function or its variables to
a php file. Since JS is so verbose I'm posting succinct code
to demonstrate my path. Everything works except the values sent
to php are zeros. Thanks in advance.

(sending the form to:)

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

<input name="btnEquals" type="Button" value="   =   " onclick="Operation('=')"></TD> 
</TR></TABLE>
<br>

(form submission)

var FKeyPad = document.Keypad; 

(this section is where I got lost)
if (value1 == parseInt(num)) // integer or decimal ?
 <input id="rNo" type="radio" name="YesNo" value="No" onclick="this.form.submit();">
<label for="rNo">calculate</label><br>

<input id="rYes" type="radio" name="YesNo" value="Yes" onClick="location.href='http://localhost/home/calcprint.php';"
<label for="rYes">print</label><br>

</FORM> 
<font face="Verdana, Arial, Helvetica" size=2> 
<SCRIPT LANGUAGE="JavaScript"> 
  {value1.value = parseInt(value1.value)}
else
   {value1.value = parsefloat(value1.value)}

   op.value = parsefloat(op.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)}

function OnCalc(value1,op,value2,total)
   {

/* var expression = purpose + value1 + op +value2 +'='+ total;
alert(expression); */
   }
</SCRIPT>

</body></html>

(this is the file I'm trying to send the variables to)

<?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 "New record created successfully"; } 
else
   { echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
mysqli_close($conn);
header( 'Location: http://localhost/home/calculator.html' );
  exit();
?> 
2
Contributors
1
Reply
14
Views
2 Years
Discussion Span
Last Post by Traevel
0

Several things I'm noticing:

You seem to be mixing JavaScript and HTML, some of it might never run.

 <input id="rNo" type="radio" name="YesNo" value="No" onclick="this.form.submit();">

<input id="rYes" type="radio" name="YesNo" value="Yes" onClick="location.href='http://localhost/home/calcprint.php';"

Both those inputs suggest the form should be sent as soon as someone clicks on them. The second one is missing a closing >.

<input name="btnEquals" type="Button" value=" = " onclick="Operation('=')"></TD> 

Apart from there not being an opening <TD> or <table> anywhere, you're calling a function called Operation('=') the code of which you did not post.

function OnCalc(value1,op,value2,total)
{
/* var expression = purpose + value1 + op +value2 +'='+ total;
alert(expression); */
}

This function is empty and never being called. Your PHP is also expecting purpose for its INSERT query, you're not passing it to this function, nor creating it here.

The function is parseFloat and not parsefloat.

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

Not sure what that is supposed to do, it just starts. There is no if. Also, parseInt won't fail, it will just put NaN in value1.value. Which I don't think is what you think it is (value1.value is probably not the form field).

if (value1 == parseInt(num))

What is value1 and what is num?

You do not have a submit button as far as I can tell and you are not posting the form in any other way than by those radioboxes.

In your PHP, you are not fetching anything from the POST data, i.e.

if(isset($_POST['value1'])){
    $value1 = $_POST['value1'];
}

Also

"INSERT INTO calculator (purpose, value1, op, value2, total)
VALUES ('purpose', 'value1', 'op', 'value2', 'total')"

You are now literally storing the words purpose, value1 etc. in your table, not any variable values. Which would be bad practice in an insert query regardless (google 'Bobby Tables' for an example).

On the bright sight, you're using mysqli instead of mysql which is good!

On the topic of all those parseInt and parseFloat's. I'm guessing your input can be either an integer or a float? parseFloat("10") will return 10. So in JavaScript there is no need for parseInt if you're already using parseFloat, unless you need it to be an integer and not a float.

To get on the right track I'd suggest you start by separating the JavaScript and HTML. Then trying to get the input values printed in an alert or to the console before sending them anywhere. That way it will be easier to spot any other issues.

If you have an input with an id value1 you could retrieve it using:

var value1 = document.getElementById("value1").value;

You could convert it to a float using value1 = parseFloat(value1);

Then check if it's correct using something like:

if(isNaN(value1)){
    alert("value 1 is not a number");
}

Since you need to do checking you would either need to wait with sending until checking is done, or check after the user has finished typing (or during). Which is why printing to screen or console will be a good step inbetween.

Edited by Traevel: sentence structure

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.