Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a form data to server using post api in angular 7

Tags:

html

angular

how can I send form data using post API using angular , have used form and method for component.ts file ,

<form #hlForm="ngForm" (ngSubmit)="getHospitalList(hlForm)">
  <input 
   type="text" 
   class="input-texts" 
   name="name" 
   placeholder="Enter Hospital , Insurance or Location"
   ngModel
   required
  >
  <button 
  type="submit" 
  value="submit" 
  class="hl-btn"
  (click)="hospitalFirstFold = false; hospitalSecondFold = true;"
   >Search
   </button>
</form>

  getHospitalList(form){
    console.log(form.value)
  }
like image 632
ishfaq Avatar asked Jul 11 '26 19:07

ishfaq


2 Answers

you need to create form data to send data using post api.

let fd = new FormData();
fd.append(key, value);

For api call

this.http.post(url, fd);

Fill values accordingly in formdata

like image 190
Mridul Avatar answered Jul 13 '26 16:07

Mridul


Start by importing the necessary APIs:

import { HttpClient } from '@angular/common/http';

Declare submit function like below:

getHospitalList(form){
    const formData = new FormData();
    formData.append('key', form.value);

    this.httpClient.post<any>(this.SERVER_URL, formData).subscribe(
      (res) => console.log(res),
      (err) => console.log(err)
    );
  }
like image 36
Sadaf Niknam Avatar answered Jul 13 '26 15:07

Sadaf Niknam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!