I learning angular 2, and I don't see any example on the web to send a simple contact form from angular 2 to php scrip.
My html template.
<form novalidate="" (ngSubmit)="guardar(forma)" #forma="ngForm">
<div class="field">
<label for="name">Nombre:</label>
<input type="text"
id="name"
name="name"
required
minlength="3"
ngModel>
</div>
<div class="field">
<label for="email">Email:</label>
<input type="email"
id="email"
name="email"
required
ngModel
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$">
</div>
<div class="field">
<label for="message">Mensaje:</label>
<textarea id="message"
name="message"
required
ngModel></textarea>
</div>
<div class="field">
<button [disabled]="!forma.valid"
type="submit">
Enviar
</button>
</div>
</form>
PHP script
<?php
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
$recipient = "nexsmedia@gmail.com";
$subject = "New contact from $name";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
$email_headers = "From: $name <$email>";
mail($recipient, $subject, $email_content, $email_headers)
?>
My incomplete angular 2 component. I already have imported in my app component HttpModule and FormsModule
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Http } from '@angular/http';
@Component({
selector: 'app-contacto',
templateUrl: './contacto.component.html',
styleUrls: ['./contacto.component.scss']
})
export class ContactoComponent {
title = 'Contacto';
constructor( private http: Http){}
url='http://myUrl.com/mailerscript.php';
name:string;
email:string;
message:string;
guardar( forma:NgForm ) {
this.name = 'name='+forma.value.name;
this.email = 'email='+forma.value.email;
this.message = 'message='+forma.value.message;
/*??*/
this.http.post(this.url, "");
}
}