1

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.

viper
  • 1,836
  • 4
  • 25
  • 41

1 Answers1

1

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

Da Mahdi03
  • 1,468
  • 1
  • 9
  • 18
  • To implement through Java code, In the html code format, where should I place the js function? I am getting `I/chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: submitMyForm is not defined", source: (1)` error – viper Jan 04 '21 at 11:47
  • Anywhere directly inside of the ` – Da Mahdi03 Jan 04 '21 at 11:48
  • 1
    If `loadUrl` isn't working for you, you can try this https://stackoverflow.com/a/31291128/9191617 – Da Mahdi03 Jan 04 '21 at 11:51
  • How would my java code know which js function to run? I mean I have kept the html and js codes in a string variable. So how would I refer to this? – viper Jan 04 '21 at 11:57
  • 1
    What do you mean? You add the function to your string variable and when you're saying `javascript:myFunction` or `evaluateJavaScript ("myFunction")` you're already telling Java which function to invoke – Da Mahdi03 Jan 04 '21 at 11:59
  • This is my implementation. So how would I pass `html` variable to webView.loadUrl("javascript:submitMyForm"); https://snippet.host/vxzm – viper Jan 04 '21 at 12:08
  • Why would you need to pass the HTML variable as well? – Da Mahdi03 Jan 04 '21 at 18:07
  • How would my webview.loadUrl() function know from where to run the js function? I am confused. Please help. – viper Jan 05 '21 at 04:40
  • It knows to run it from the file since your function is already defined there – Da Mahdi03 Jan 05 '21 at 08:35