To make it work, you should think in React!
That means that you should always keep in mind that React components act in the virtual DOM and the most reliable way to handle the form submission is to keep your form in Virtual DOM as a React Component.
Instead of using onLoad event you should use lifecycle events in React that also corresponds to the virtual DOM. In the example, I used componentDidMount to determine the moment when the root component is mounted (loaded) in the virtual DOM.
class Form extends React.Component {
state = {
termUrl: 'http://www.url.gr/return.php',
md: 'NBGTEST000000412',
}
componentDidMount() {
this.props.onSubmit({
termUrl: this.state.termUrl,
md: this.state.md,
});
}
handleSubmit = (e) => {
e.preventDefault();
this.props.onSubmit({
termUrl: this.state.termUrl,
md: this.state.md,
});
}
render() {
return (
<form
name="threedfrom"
onSubmit={this.handleSubmit}
>
<h3>Form</h3>
<textarea style={{ display: 'none' }} name="PaReq"></textarea>
<span>*Mετά τον ACS για το capture του PARes</span>
<input
type="hidden"
name="TermUrl"
value={this.state.termUrl}
/>
<input
type="hidden"
name="MD"
value={this.state.md}
/>
</form>
);
}
}
class App extends React.Component {
submit = (values) => {
console.log('Submitting', values);
const url = 'https://accreditation.datacash.com/acs-nbgs2a_i';
fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: values,
})
.then((resp) => { console.log('Got response', resp); })
.catch((err) => { console.error('Got error', err); })
}
render() {
return (
<div>
<h2>React App</h2>
<Form onSubmit={this.submit} />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('react'));
<html>
<head>
<title>Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
</head>
<body>
<div id="react"></div>
</body>
</html>