[英]Can't pass hidden input value from Angular to PHP
我创建了一个表格,希望用户在其中选择他们想要的房间。 如果我将其作为下拉列表或输入信息的输入类型来执行,则通过PHP通过电子邮件提交。
但是,如果我将输入类型更改为“ hidden”,则该值将完全消失,并且在控制台中看到的是:
<input _ngcontent-c4 name="room" type="hidden" value class="ng-untouched ng-pristine ng-valid">
然后,在网络中,没有任何内容显示为“房间”的对象。 (注意:确实会出现其他对象。)
然后,如果我只是将样式更改为“ display:none;”。 并输入值=“ ChosenRoom”,则电子邮件中将不会发送任何内容。 实际上,尽管在控制台中清楚地显示为
<input _ngcontent-c4 name="room" style="display: none;" type="text" value="ChosenRoom" class="ng-untouched ng-pristine ng-valid">
该对象未显示在网络中。
知道可能是什么问题吗?
码:
(我也将其发布在基本的Plunker中: http ://embed.plnkr.co/uUhPrStdw4kDhaus2xfO/)
此功能有效, 但用户必须以以下形式输入房间名称 :
HTML(注意:在这种情况下,与表单无关,我忽略了该表单的其余代码。如果您认为这会有所帮助,我也很乐意将其发布。)
<form (submit)="sendEmail(message)" #f="ngForm">
<input type="text" name="room" [(ngModel)]="message.room" #room="ngModel">
<button type="submit" [disabled]="f.invalid" *ngIf="!f.submitted">
</form>
HTML-此内容未在控制台中显示该值。
<form (submit)="sendEmail(message)" #f="ngForm">
<input type="hidden" name="room" [(ngModel)]="message.room" #room="ngModel" value="ThisRoom">
<button type="submit" [disabled]="f.invalid" *ngIf="!f.submitted">
</form>
HTML-此代码显示控制台中的值,但不显示网络中的任何对象。
<form (submit)="sendEmail(message)" #f="ngForm">
<input type="text" style="display: none;" name="room" [(ngModel)]="message.room" #room="ngModel" value="ThisRoom">
<button type="submit" [disabled]="f.invalid" *ngIf="!f.submitted">
</form>
打字稿:Contact.Service.TS
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Resolve } from '@angular/router';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
export interface IMessage {
name?: string,
email?: string,
room?: string,
daterange?: string,
message?: string
}
@Injectable()
export class AppService {
private emailUrl = '/assets/email.php';
constructor(private http: Http) {
}
sendEmail(message: IMessage): Observable<IMessage> | any {
return this.http.post(this.emailUrl, message)
.map(response => {
console.log('Sending email was successfull', response);
return response;
})
.catch(error => {
console.log('Sending email got error', error);
return Observable.throw(error)
})
}
}
的PHP
<?php
header('Content-type: application/json');
$errors = '';
if(empty($errors))
{
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$from_email = 'general@mywebsite.com';
$message = $request->message;
$from_name = $request->name;
$to_email = "myemail@gmail.com";
$contact = "<p><strong>Name:</strong> $from_name</p>
<p><strong>Email:</strong> $request->email</p>
<p><strong>Room:</strong> $request->room</p>
<p><strong>Dates:</strong> $request->daterange</p>";
$content = "<p><strong>Message:</strong><p>$message</p>";
$website = 'My Currently Not but soon to be Functioning Website';
$email_subject = "$website: Received a message from $from_name";
$email_body = '<html><body>';
$email_body .= "$contact $content";
$email_body .= '</body></html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $from_email\n";
$headers .= "Reply-To: $from_email";
$result = mail($to_email,$email_subject,$email_body,$headers);
$response_array['status'] = 'success';
$response_array['from'] = $from_email;
$response_array['result'] = $result;
echo json_encode($response_array);
header($response_array);
return $from_email;
} else {
$response_array['status'] = 'error';
echo json_encode($response_array);
header('Location: /error.html');
}
?>
我感谢任何帮助或想法。
谢谢!
布拉德
我已经找到了解决该问题的方法。 它完全避免了隐藏字段,我仍然不确定它们是否是问题所在。
工作解决方案:
contact.component.html-在这里,我避免隐藏输入,并在消息旁边传递文本。 特别注意“这个房间”
<form (submit)="sendEmail(message, 'This particular room')" #f="ngForm">
...
</form>
contact.component.ts-特别注意message.room = room;
import { Component, OnInit, HostBinding } from '@angular/core';
import { AppService, IMessage } from '../../contact/contact.service';
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.css']
})
export class TheensuiteComponent implements OnInit {
message: IMessage = {};
constructor(private appService: AppService) { }
sendEmail(message: IMessage, room:string) {
message.room = room;
this.appService.sendEmail(message).subscribe(res => {
console.log('ContactComponent Success', res);
}, error => {
console.log('ContactComponent Error', error);
})
}
ngOnInit() {
};
}
}
其他所有内容均保持不变(contact.service.ts和email.php)。
当然,这是一个小小的黑客。 如果我想传递更多信息,这可能会导致代码混乱。
首先,未添加值是因为ngModel
覆盖了value
,因此与隐藏的事实无关。 其次,我建议您可能要在此处使用模型驱动的表单。 有时,模型驱动的表单过于夸张,但是在这种情况下,我会使用一个表单。 因此,如果您考虑考虑,我本想为您提供该选项。
您需要导入ReactiveFormsModule
并将其添加到ngModule中的imports
数组中。 然后将以下内容导入到您的组件中,并将FormBuilder
注入您的构造函数中:
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
constructor(private fb: FormBuilder) {}
然后使用所需的验证器构建表单:
this.myForm = this.fb.group({
room: ['theEnsuite'],
name: ['', Validators.required],
email: ['', [Validators.required, Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$']],
message: ['', [Validators.required, Validators.minLength(10)]]
})
然后您可以摆脱双向绑定,而改为使用表单控件。 好消息是,您根本不需要在模板中包含room
。 它已经很好地位于表单对象中了:)这是带有错误消息的name
输入示例:
<input placeholder="Name" formControlName="name">
<div *ngIf="!myForm.get('name').valid && myForm.get('name').touched">
<p>Please enter your name.</p>
</div>
提交表单时,请使用myForm.value
作为参数。 然后,该对象与IMessage
类型的对象相同,您可以直接将post请求与此对象一起使用!
这是上面的演示 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.