0

I have a simple form:

<form action="" method="POST"> 
  <input type="text" name="arg1"/>
  <input type="text" name="arg2"/>
  <input type="submit"/>
</form> 

Then when it's submitted, I POST arg1 via cURL and update its value based on the cURL response data. I then call another function to change arg2 using a call to a Java program with exec.

And these two updated arg1 and arg2 should be the value attributes for that form after I submit it initially. (So that after the first submit, I just need to click "submit" as long as I keep getting data from the cURL request)

I tried using global as seen in this post: Giving my function access to outside variable ,but it doesn't work for me

My code is something like that:

   <form action="" method="POST"> 
      <input type="text" name="arg1" value="<?php echo $arg1;?>"/>
      <input type="text" name="arg2" value="<?php echo $arg2;?>"/>
      <input type="submit"/>
    </form> 



<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {    

    $arg1_post = $_POST["arg1"];
    $arg2_post = $_POST["arg2"];
    $curl_post_fields = '{"arg1":' . $arg1_post . ', "arg2": "'. $arg2_post . '"}';
    
    sendCurlPost($curl_post_fields);

}

function sendCurlPost($curl_post_fields){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,            "https://url/" );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST,           1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS,     $curl_post_fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 

    if ($response=curl_exec($ch)){
        global arg1;
        $arg1 = // some value from the $response

        $output=null;
        $retval=null;
   
        runJavaProgram();

    }
}

function runJavaProgram (){
    exec('C:\Java\jdk-18\bin\java.exe Main.java 2>&1 ', $output, $retval);   
    
    global $arg2;
    $arg2 = $output[0];

I keep getting Undefined variable even after the first POST. I also tried other methods using SESSION and COOKIE but that didn't work either

pileup
  • 1
  • 2
  • 18
  • 45
  • 1
    Which one is the undefined variable? – nice_dev Oct 26 '21 at 16:28
  • @nice_dev Both are – pileup Oct 26 '21 at 16:31
  • 1
    You mean $arg1 and $arg2? – nice_dev Oct 26 '21 at 16:47
  • Yes sorry about that (I see the `Undefined variable` message inside the form fields instead of the actual value) – pileup Oct 26 '21 at 16:47
  • 2
    Can you share full code if the file? – nice_dev Oct 26 '21 at 16:49
  • It's very long I will try to remove some unecessary stuff. But I think the issue could be the fact that I first POST the form, THEN I calculate the new values for `$arg1` and `$arg2`? So first the page refreshes, and only then it updates their values thus they're not appearing? – pileup Oct 26 '21 at 16:52
  • 1
    Where does `$arg2` come in? Should `runJavaProgram` have `$_POST["arg2"]`? – user3783243 Oct 26 '21 at 16:53
  • @user3783243 oh so you mean, once I get the value from the Java program, I should POST again? And this time post with both `$arg1` and `$arg2` after they've been updated? It's like two stages that require two post requests? One from the form, and then from the code? Also, nice_dev I updated the code and it really is almost the same as mine just without the calculations. The values in my real code are right because when I echo them it's the correct values – pileup Oct 26 '21 at 17:03

0 Answers0