I'd like to send form data to a server however I can't send the form value and the button name to distinguish between the button call.
Here is my code:
<div class="widgets" id="top" hidden="true">
<ba-card title="Update User" baCardClass="with-scroll" >
<div class='row'>
<div class='col-md-6'>
<form [formGroup]="complexForm" (ngSubmit)="submitForm(complexForm.value)">
<!-- FORM DATA -->
<button type="submit" value="update" [disabled]="!complexForm.valid" class="btn btn-success">Update User</button>
</div>
<div class="form-group">
<button type="submit" value="delete" class="btn btn-success">Delete User</button>
</div>
</form>
</div>
</div>
</ba-card>
My Backend call for submit form:
updateUser(value: any) {
const _url
const headers = new Headers();
headers.append('X--User', sessionStorage.getItem('username'));
headers.append('X--Token', sessionStorage.getItem('token'));
headers.append('X--AccessTime', sessionStorage.getItem('AccessTime'));
headers.append('Content-Type', 'application/json');
const options = new RequestOptions({ headers: headers });
return this.http.post(_url, value, options)
.subscribe((response: Response) => {
});
}
deleteUser(value: any) {
const _url
const headers = new Headers();
headers.append('X--User', sessionStorage.getItem('username'));
headers.append('X--Token', sessionStorage.getItem('token'));
headers.append('X--AccessTime', sessionStorage.getItem('AccessTime'));
headers.append('Content-Type', 'application/json');
const options = new RequestOptions({ headers: headers });
return this.http.post(_url, value, options)
.subscribe((response: Response) => {
});
}
So my problem lies here, I bind ngSubmit to updateUser(complexForm.value), complexForm.value contains all the form data I want to send to the backed. I have Update user and delete user that both require the form data. If I also bind ngSubmit to deleteUser() it will just call both updateUser and deleteUser.
I saw a solution of adding a name to each button, however I couldn't send the button value to the backend along with the form value (since I'm doing the call with ngSubmit).
Thank you.