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