我创建了一个表单,它在角度中使用离子复选框,当勾选时将“true”提交给数据库,我如何将框的值提交给数据库,例如在我的列表中是 Junior Groups,所以我会像“少年组”出现在数据库中而不是真实的。该表格还会发送一封电子邮件,因此收件人需要知道查询感兴趣的内容。
到目前为止的代码是:
html表单:
<form [formGroup]="coachingform">
<ion-grid>
<ion-row>
<ion-col>
<ion-item>
<ion-label position="start">First Name</ion-label>
<ion-input type="text" formControlName="firstname"></ion-input>
</ion-item>
<ion-item>
<ion-label position="start">Last Name</ion-label>
<ion-input type="text" formControlName="surname"></ion-input>
</ion-item>
<ion-item>
<ion-label position="start">Email</ion-label>
<ion-input type="email" formControlName="email"></ion-input>
</ion-item>
<ion-item>
<ion-label position="start">Contact Number</ion-label>
<ion-input type="tel" formControlName="telnumber"></ion-input>
</ion-item>
<ion-item>
<ion-label position="start">What city are you based in?</ion-label>
<ion-input type="text" formControlName="location"></ion-input>
</ion-item>
<ion-list>
<ion-label>What are you interested in?</ion-label>
<ion-item *ngFor="let entry of form">
<ion-label>{{entry.val}}</ion-label>
<ion-checkbox slot="start" formControlName="interestedin"></ion-checkbox>
</ion-item>
</ion-list>
然后在 .ts 文件中有这样的列表:
export class CoachingenquiryPage implements OnInit {
coachingform: FormGroup
public form = [
{ val: 'Junior Groups', isChecked: true },
{ val: 'Adult Groups', isChecked: true },
{ val: 'Individual Lessons', isChecked: true },
{ val: 'Holiday Camps', isChecked: true },
];
任何帮助将不胜感激。非常感谢
更新的 .ts 代码是:
import { Component, OnInit } from '@angular/core';
import { FormControl, FormBuilder, FormGroup, Validators, FormArray }
from '@angular/forms'
import { HttpClient, } from '@angular/common/http';
import { Router } from '@angular/router';
@Component({
selector: 'app-coachingenquiry',
templateUrl: './coachingenquiry.page.html',
styleUrls: ['./coachingenquiry.page.scss'],
})
export class CoachingenquiryPage implements OnInit {
coachingform: FormGroup
public options= [
{ val: 'Junior Groups', isChecked: true },
{ val: 'Adult Groups', isChecked: true },
{ val: 'Individual Lessons', isChecked: true },
{ val: 'Holiday Camps', isChecked: true },
];
constructor(private http: HttpClient, public fb: FormBuilder, private
router: Router) {
this.coachingform = this.fb.group({
firstname: [''],
surname: [''],
email: [''],
telnumber: [''],
location: [''],
interestedTopics: this.fb.array([]),
})
const control = this.coachingform.get("interestedTopics") as
FormArray;
this.options.forEach(f => {
const group = this.fb.group({
interestedin: [f.isChecked]
});
control.push(group);
});
}
ngOnInit() {
this.coachingform = new FormGroup({
firstname: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required]
}),
surname: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required]
}),
email: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required, Validators.email]
}),
telnumber: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required, Validators.pattern('[- +()0-9]
{11,}')]
}),
location: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required]
}),
interestedin: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required]
}),
})
}
onSubmitForm () {
var formData: any = new FormData();
formData.append("firstname",
this.coachingform.get('firstname').value);
formData.append("surname", this.coachingform.get('surname').value);
formData.append("email", this.coachingform.get('email').value);
formData.append("telnumber",
this.coachingform.get('telnumber').value);
formData.append("location", this.coachingform.get('location').value);
formData.append("interestedin",
this.coachingform.get('interestedin').value);
this.http.post('https://www.parktennis.org/app/services/app-
coachingenquiry.php', formData, {responseType: 'text'}).subscribe(()
=> {
this.coachingform.reset();
this.router.navigate(['/','landing']);
});
}
}