need help w/passing JS to php

12Strings

Solid State Member
Messages
8
Location
at home
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"

Code:
<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:
<?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":
PHP:
$query = "
INSERT INTO calculator (purpose, value1, op, value2, total)
VALUES ('$purpose','$value1','$op','$value2','$total')";
mysqli_query($con, $query);
mysqli_close($con); 
   ?
>
Code:
<a href="http://localhost/home/calc_print.php">Print</a>
</center></body></html>
--------------------------------------------------------
 
What does your "OnCalc" function do?

In your PHP, you're not getting any form data at all; you're just sending in new/empty variables in your query string. You need to get the data from the form via POST, if that's the method you're using for your form's submit.
 
HI, I'm worrying with a calULATOR, trying to send to
& print out a list of entries.
Doing an alert of the values in this following code
presents the right values but no results. Actually,
my little project is to enter a record (value1, op, value2
& total) into database table as in a paper tape calculator
w/each submit. Upon completion, clicking "print", presents
a report. I've seen & tried many enquiries without resolution.
I'm obviously not understanding the concept.Any help?

Code:
<script>
// create the XMLHttpRequest object, according browser
function get_XmlHttp() {
  // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
  var xmlHttp = null;

  if(window.XMLHttpRequest) {        
// for Forefox, IE7+, Opera, Safari, ...
    xmlHttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {    
// for Internet Explorer 5 or 6
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xmlHttp;
}

// sends data to php via GET and POST-displays the received answer
function ajaxrequest(php_file, tagID)
 {
   var request =  get_XmlHttp();           
// call the function for the XMLHttpRequest instance
// create URL w/data to send to server, via GET (a pair index=value)
  var  url = php_file+ '?id='+document.getElementById('id').innerHTML;
// create pairs index=value w/data that must be sent server, via POST
  var d_post = 'value1='+document.getElementById('value1').value;
  request.open("POST", url, true);            
// sets the request
// adds a header-tell PHP script to recognize data as is sent via POST
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.send(d_post);          
// sends the request
  // Check request status
// If response rec'd comple, will be transferred to HTML tag w/ tagID
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      document.getElementById(tagID).innerHTML = request.responseText;
    }
  }
}
    </head>
<form action="calcinsert.php" method="post" name="keypad" onsubmit="ajaxrequest('calcinsert.php', 'resp'); return false;">

<input type="text" id="purpose" name="purpose" size="48" value="what's 
  <blah, blah, code ****************************************

</TR></TABLE>
 <input type="submit" value="Send" /><p>
</FORM> 
      
    <font face="Verdana, Arial, Helvetica" size=2> 
    <SCRIPT> 
    var FKeyPad = document.Keypad;
 
     <blah, blah, code ***************************************
     
{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:
  <?php
      header( "refresh:5;url='http://localhost/home/calcprint.php'");
      echo 'You\'ll be redirected in about 5 secs. If not, click <a href="http://localhost/home/calcprint.php">here</a>.'; 

    include ('gethomedb.php');
    // now connected to database
        if(!empty($_POST["submit"])) 
     {
    echo '<script>'
       , '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); 
     }
      }
    ?>
 
I believe you need to have your header() call at the bottom of your PHP file since that's what's redirecting - if it's redirecting right away, you're not processing the rest of your PHP file. Been a while since I've done anything with PHP, so that's just an assumption.
 
Hi, I got led astray chasing my tail re. ajax, etc. In the back of my mind
I kept thinking I had done this b4. Ok, this project only requires JS &
php. I just need to learn how to send a function or its arguments to
my 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:)
Code:
<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)
Code:
<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"> 
var FKeyPad = document.Keypad;
(this section is where I got lost)
Code:
if (value1 == parseInt(num)) // integer or decimal ?
   {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:
<?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();
?>
 
You're not populating the values in your $sql variable from your POST data.

You need to create PHP variables, populate them from POST, and then use those variables in your $sql variable for the values.
 
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.
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:
<?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;
?>
 
Back
Top Bottom