1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

need help w/passing JS to php

Discussion in 'Software Development' started by ckir5951, Jan 19, 2015.

Thread Status:
Not open for further replies.
Advertisement
  1. ckir5951

    ckir5951 Thread Starter

    Joined:
    Nov 16, 2014
    Messages:
    27
    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();
    ?> 
     
  2. PcPhoenix

    PcPhoenix

    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:


    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.
     
  3. ckir5951

    ckir5951 Thread Starter

    Joined:
    Nov 16, 2014
    Messages:
    27
    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??
     
  4. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    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.
     
  5. ckir5951

    ckir5951 Thread Starter

    Joined:
    Nov 16, 2014
    Messages:
    27
    thanks, I'll try that
     
  6. ckir5951

    ckir5951 Thread Starter

    Joined:
    Nov 16, 2014
    Messages:
    27
    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>
     
  7. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1141486

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice