I have created a form in which I would like users to select the room that they want, among other things. If I do this as a dropdown or as an input type in which they type in the information, it is submitted via email through PHP.
However, if I change the input type to "hidden", the value completely disappears and what I see in the console is this:
<input _ngcontent-c4 name="room" type="hidden" value class="ng-untouched ng-pristine ng-valid">
Then, in the Network, nothing appears as an object for "room". (Note: other objects do appear.)
Then, if I simply change the style to "display: none;" and put the value="ChosenRoom", nothing is sent in the email. In fact, despite showing up clearly in the console as
<input _ngcontent-c4 name="room" style="display: none;" type="text" value="ChosenRoom" class="ng-untouched ng-pristine ng-valid">
the object doesn't show up in the network.
Any idea what could be the problem?
CODE:
(I have posted it in a rudimentary Plunker as well: http://embed.plnkr.co/uUhPrStdw4kDhaus2xfO/)
This one works, but the user has to type in the room name in the form:
HTML (Note: I left out the remaining code for the form, assuming that it isn't relevant in this case. If you think this would help, I'd be happy to post that as well.)
<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 - This one doesn't show the value in the console.
<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 - This one shows the value in the console, but does not show any object in the Network.
<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>
Typescript: 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 = '[email protected]';
$message = $request->message;
$from_name = $request->name;
$to_email = "[email protected]";
$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');
}
?>
I appreciate any help or ideas.
Thanks!
Brad
ngModeloverridesvalue. Also it doesn't really make even sense to have two-way-binding, since you could very well get the same object from the form :) Also, in this case I'd maybe suggest a model-driven-form, the hidden property would be easy to create with that :) If you want to stick to the template-driven form, I'd suggest you set the default value to theroominstead.