need help w/passing JS to php

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

ckir5951

Thread Starter
Joined
Nov 16, 2014
Messages
39
Hi, I got led astray with the ajax, etc. I'm sure this only requires JS and
php. I just don't know how to send my 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:)
HTML:
<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)
HTML:
<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>
HTML:
</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>
HTML:
</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();
?>
 
Joined
Jan 18, 2015
Messages
99
You pass functions to PHP either using GET or POST.

With GET, you use the URL to pass values and associate them with parameters.
With POST, you embed the values and parameters into the http request data.

Lets use GET because it's simpler.

This is what passing parameters to a PHP file using GET looks like:

http://www.testwebsite.com/somephpfile.php?param1=one&param2=two&param3=three

Using this URL you pass 3 values to somephpfile.php: one, two, three. These went to the parameters param1, param2, and param3 respectively.

Now, in your PHP, to access these values, you use $_GET["param1"] and $_GET["param2"] & $_GET["param3"]

So, assuming you will be passing purpose, value1, op, value2, and total to your PHP code, it should look like this:

<?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 ('$_GET['purpose']', '
$_GET['value1']', '$_GET['op']', '$_GET['value2']', '$_GET['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();
?>

Now, in HTML when you create a "form", you specify parameters and their names, you specify either GET or POST, and you specify a PHP file. So when the form is filled out and "submitted", it builds this URL with the parameters and values entered on the form, and takes you to that PHP file. You don't need any javascript for this.

If you need to use javascript, you need to trigger a javascript function when the form is submitted. This javascript function should extract the parameters and values from the form, build the URL manually, and direct the user's browser there.
 

ckir5951

Thread Starter
Joined
Nov 16, 2014
Messages
39
thanks friend. what's got me stumped is that what I'm trying to submit is javascript function arguments
(function OnCalc(purpose,value1,op,value2,total) and nothing I've tried
has worked??
 

Ent

Josiah
Retired Trusted Advisor
Joined
Apr 11, 2009
Messages
5,467
You cannot send the javascript function to the server and expect it to run it for you, for the very simple reason that PHP doesn't speak javascript.
Instead you have two options:
You could run the function on the client computer, and send the result of the function to php.
Since you're (fairly) trying to avoid using AJAX, the easiest way to do this is to use a hidden input.

That is, in the HTML of the form, you have something like
Code:
<input id="equationInput" type="hidden" name="equation" value="" >
Then in the javascript, at some point you guarantee that you have the line
Code:
document.getElementById("equationInput").value = OnCalc(purpose,value1,op,value2,total)
Obviously OnCalc() would need to actually return something.

The alternative is simply to submit the values that exist in the form, and rewrite the function you want to run in your php script.
 

ckir5951

Thread Starter
Joined
Nov 16, 2014
Messages
39
Is this what ur suggesting?

HTML:
<td>
                    <input name="btnEquals" type="Button" value=" = " onclick="Operation('=')" />
                </td>
            </tr>
            <tr>
<td><input id="equationInput" type="hidden" name="equation" value="" ></td>
    <td colspan="6" style="text-align: center">
    <input id="submit" type="button" value="Submit" />
                </td>
            </tr>
-------------------------------------------------------
Code:
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    xhr.send("data="+JSON.stringify($data));
                }
                return false;
            };
        })();
document.getElementById("equationInput").value = OnCalc(purpose,value1,op,value2,total)

    </script>
<br><a href="http://localhost/home/calcprint.php">print</a>
HTML:
</body></html>
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top