Reputation: 1906
I have the following implementation in my html code.
<!doctype html>
<html ng-app="app">
<head>
<title>post data</title>
</head>
<body>
<div>
<form action="https://mypaymentgateway/pay" method="post">
<input type="hidden" id="profile_id" name="profile_id" value="123" />
<input type="hidden" id="transaction_id" name="transaction_id" value="abcd" />
<input type="hidden" id="locale" name="locale" value="en" />
<input type="submit" id="btnsubmit" value="Confirm" />
</form>
</div>
</body>
</html>
What I want to do is submit the above values in webview by calling the POST method of my payment url ("https://mypaymentgateway/pay") without having to tap the 'confirm' button. I have tried solutions here and here and other sources too. But nothing seems to be working correctly. Please help me fix this issue.
Upvotes: 1
Views: 1964
Reputation: 1608
Try invoking form.submit()
on your own if you want to POST the form without having to press the button
There are 2 ways you can do this depending on how you need it done, through JavaScript on the HTML file itself or through your Java file that enables the WebView component.
Through the JavaScript in the HTML file:
<form id="myForm" action="https://mypaymentgateway/pay" method="POST"> ... </form>
<script>
function submitMyForm() {
var myForm = document.querySelector("#myForm");
myForm.submit();
}
</script>
And you would just call submitMyForm()
in the JavaScript wherever you need it.
Through the Java file (have the previous function defined in the global scope of your JavaScript in your HTML file):
//First enable your JavaScript
webView.getSettings().setJavaScriptEnabled(true);
//And then with a loadURL you can trick the WebView into executing Javascript
webView.loadUrl("javascript:submitMyForm");
Credit to: Android Calling JavaScript Function in WebView
Upvotes: 1