Hi!
I need help with the following code :
$admin_email = 'myemail@hotmail.com'; // Your Email
$message_min_length = 5; // Min Message Length
class Contact_Form{
function __construct($details, $email_admin, $message_min_length){
$this->name = stripslashes($details['name']);
$this->email = trim($details['email']);
$this->subject = 'Contacto desde Formulario'; // Subject
$this->message = stripslashes($details['message']);
$this->email_admin = $email_admin;
$this->message_min_length = $message_min_length;
$this->response_status = 1;
$this->response_html = '';
}
private function validateEmail(){
$regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
if($this->email == '') {
return false;
} else {
$string = preg_replace($regex, '', $this->email);
}
return empty($string) ? true : false;
}
private function validateFields(){
// Check name
if(!$this->name)
{
$this->response_html .= '<p>Nombre</p>';
$this->response_status = 0;
}
//Check privacy
if(!$this->privacy)
{
$this->response_html .= '<p>Por favor acepta el Aviso de Privacidad</p>';
$this->response_status = 0;
}
// Check email
if(!$this->email)
{
$this->response_html .= '<p>Por favor ingresa un e-mail valido</p>';
$this->response_status = 0;
}
// Check valid email
if($this->email && !$this->validateEmail())
{
$this->response_html .= '<p>Por favor ingresa un e-mail valido</p>';
$this->response_status = 0;
}
// Check message length
if(!$this->message || strlen($this->message) < $this->message_min_length)
{
$this->response_html .= '<p>Mensaje muy corto, debe de ser de al menos 5 caracteres </p>';
$this->response_status = 0;
}
}
private function sendEmail(){
$mail = mail($this->email_admin, $this->subject, $this->message,
"From: ".$this->name." <".$this->email.">\r\n"
."Reply-To: ".$this->email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
$this->response_status = 1;
$this->response_html = '<p>Gracias, tu mensaje ha sido enviado!</p>';
}
}
function sendRequest(){
$this->validateFields();
if($this->response_status)
{
$this->sendEmail();
}
$response = array();
$response['status'] = $this->response_status;
$response['html'] = $this->response_html;
echo json_encode($response);
}
}
$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();
The html code:
<form id="contact-form" class="contact-form" action="#">
<p class="contact-name">
<input id="contact_name" type="text" placeholder="Nombre Completo" value="" name="name" />
</p>
<p class="contact-email">
<input id="contact_email" type="text" placeholder="Correo Electronico" value="" name="email" />
</p>
<p class="contact-message">
<textarea id="contact_message" placeholder="Tu Mensaje" name="message" rows="15" cols="40"></textarea>
</p>
<table width="80%">
<tr><td height="48"> </td><td width="20px;"><input name="privacidad" type="checkbox" value="Si" /></td><td>He leido y acepto el <a href="avisodeprivacidad.html" target="_new">Aviso de Privacidad</a></td></tr>
</table>
<a id="contact-submit" class="submit" href="#"><span style="display:block; padd-top:-20px; color:#000;">
<p class="contact-submit"><span style="font-size:13px;"><br /></span>
Enviar formulario
</p></span>
</a>
<div id="response">
</div>
</form>
The code worked fine but when i added the validation "privacy" stop sending mail. Any ideas?
Thank You!
*Sorry for my English