2

I am coding a php/html page, with some javascript functions.

I want to perform an INSERT on my MySQL Database if the result of one of my javascript functions is equal to 1.

01) How can I access the result of this javascript function in PHP code?

02) When executing this function, I can set the value of a HTML text form, but I get "Undefined index" when I try to get this value by $_GET['value']. Any sugestions here?

Thanks in advance!

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script language="javascript">
        function function2( a, b, c, d, e, f, g, h)
        {
            //calculations here
            var elem = document.getElementById("result");
            elem.value = result;    
            return result;
        }

        function function1 ()
        {
            //set values of form1
            function2();
        }  
    </script>  
</head>
<body>
    <script>
        window.onload = function() 
        {
            function1();
        }    
    </script>     

    <?php 
        //MySQL to get a, b, c, d, e, f values
    ?>

    <form name="form1" id="form1" method="get">
        <input type="hidden" name="g" value=""> </input>
        <input type="hidden" name="h" value=""> </input>
    </form>

    <form name="form2" id="form2" method="get">
        <input type="text" name="a" value="<?php echo $a; ?>"> </input>
        //same to b, c, d, e, f
        <input type="text" id='result' name="result" value="0"> </input>
    </form>

    <?php
        //perform MySQL operation if function2 == 1
        //or if $_GET['result'] == 1
    ?>
</body>
</html>
krlzlx
  • 5,752
  • 14
  • 47
  • 55
  • 2
    Try using `AJAX`. The error that you are getting means that the key `value` in `$_GET` has not been set in your `PHP` code. – Script47 Oct 31 '15 at 22:20
  • 3
    You need [tag:ajax]. – Zsw Oct 31 '15 at 22:21
  • For your second question, are you sure you're submitting your form with the GET method and not the POST method? – Pavlin Oct 31 '15 at 22:23
  • If you are submitting via a `POST` you will need to use `$_POST` to access the form values. – doublesharp Oct 31 '15 at 22:24
  • But regardless you should use `isset()` to check if the index is set before trying to access the value. – doublesharp Oct 31 '15 at 22:24
  • And `!empty(trim());`. – Script47 Oct 31 '15 at 22:26
  • Actually, the key of the GET field is "result", as in the part of the code I pasted in the question. It is inside form2, and is successfully set by function2. – Max Schroedinger Oct 31 '15 at 23:01
  • @Script47 this will work if PHP version is 5.5 upwards. See manual for empty() – Arif Nov 01 '15 at 01:11
  • @Arif Actually it is 5.4 however I never said it won't? I was merely suggesting better ways to validate. – Script47 Nov 01 '15 at 01:12
  • @Script47 hmm, I'm just filling the gap for missing info to your suggestion, as the purpose is to guide someone not knowing this. – Arif Nov 01 '15 at 01:16
  • @Arif well I assumed the person would look up the function to find out what it does. They'd read there all the relevant information. ;) – Script47 Nov 01 '15 at 01:18
  • @Script47 hah! Ha ... In that case we may need not the whole community here. Btw, I was saying specifically because this use case of trim inside empty was quite a common confusion for anyone not knowing upfront, there is a lot of discussion on web about this - e.g. http://stackoverflow.com/questions/16988388/emptytrim-postusername which PHP finally took care of. Nonetheless, my personal opinion is to not assume when suggesting a novice (the question indicates this), or at least mention what you assumed – Arif Nov 01 '15 at 01:28

1 Answers1

0

If you are picking up "Undefined" then the php part might be working - result looks like it might actually be undefined though.

try

var result = your results from calculations;

I also think you also might also want your "GET" to be looking at GET['g'], GET['h'] the names of the hidden inputs on form1

Does function1 pass a,b,c etc to function2() as its () look empty?

A second thought - you might try submitting to another processing page - the GET needs to get the data from the urlencoded query string I think.

So you could add an action to your form at the top pointing to your processing page action="processing.php and a submit button at the bottom of the form and then put all of the php processing part in a page called processing.php which does your MySQL to put the results back into the database. Duly sanitised, of course!

   <?php
   //perform MySQL operation if function2 == 1
   //or if $_GET['result'] == 1
   ?>

Goes on that page together with any html you want to use to show if the process has been successful.

If you don't want a button, you could make your form into a method="POST" form and submit it with JavaScript submit() once your last function finishes. A little more secure that way too. You would need to look for $_POST['result'] instead of $_GET['result'] as per other comments above. I also think you could manage all of that with just one form.

Steve
  • 808
  • 1
  • 9
  • 14