Currently, I have a java .class
file that I want to run when a button is pressed and PHP exec()
command calls it.
Say I have this in a .class file:
class Test {
public static void main(String[] args) {
String a = args[0];
System.out.println(a);
}
}
Now I want PHP to execute this java using the exec()
function. However, this class file needs parameters. I want to feed parameters through a form and submit button.
To the visitors, upon loading the site they would see the average form + submit:
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
Once they hit the submit button, I want the button to call PHP command
exec(java test username lastname)
given "username" and "lastname" is generated by the visitor.
</div>
First of all, add method="post"
to your form:
<form action="action_page.php" method="post">
Then on submit you will be able to access the form inputs using the $_POST
variable. Make sure you call escapeshellarg()
on user input variables.
$username = escapeshellarg($_POST['username']);
$lastname = escapeshellarg($_POST['lastname']);
And then you can use exec()
:
exec("java test $username $lastname");