
AYUDAAA con mi formulario online por favor
Publicado por Orlando (5 intervenciones) el 22/02/2016 09:05:42
Hola,este es mi código de mi formulario online,y no me funciona pero no consigo ver qué está mal,por favoor necesito alguien me ayude,gracias con antelación
######################################
HTML
#############################################
####################################
JS
#####################################
-incluir jQuery.min.js
-codigo de mi js:
####################################
PHP
####################################
######################################
HTML
#############################################
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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="Email" value="" name="email" />
</p>
<p class="contact-message">
<textarea id="contact_message" placeholder="Escriba aquí su mensaje" name="message" rows="15"></textarea>
</p>
<p class="contact-submit">
<a id="contact-submit" class="submit" href="#">Enviar Email</a>
</p>
<div id="response">
</div>
</form>
####################################
JS
#####################################
-incluir jQuery.min.js
-codigo de mi js:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$("#contact-submit").on('click',function() {
$contact_form = $('#contact-form');
var fields = $contact_form.serialize();
$.ajax({
type: "POST",
url: "contact.php",
data: fields,
dataType: 'json',
success: function(response) {
if(response.status){
$('#contact-form input').val('');
$('#contact-form textarea').val('');
}
$('#response').empty().html(response.html);
}
});
return false;
});
####################################
PHP
####################################
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* Contact Form Class
*/
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
$email_admin = 'orlandoabreu221@gmail.com'; // Your Email
$message_min_length = 1; // 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 a traves de su web'; // 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>Por favor ingrese su nombre</p>';
$this->response_status = 0;
}
// Check email
if(!$this->email)
{
$this->response_html .= '<p>Por favor ingrese su email</p>';
$this->response_status = 0;
}
// Check valid email
if($this->email && !$this->validateEmail())
{
$this->response_html .= '<p>Por favor ingrese un email válido</p>';
$this->response_status = 0;
}
// Check message length
if(!$this->message || strlen($this->message) < $this->message_min_length)
{
$this->response_html .= '<p>Por favor ingrese su mensaje</p>';
$this->response_status = 0;
}
}
private function sendEmail(){
$mail = mail($this->email_admin, $this->subject, $this->message,
"De: ".$this->name." <".$this->email.">\r\n"
."Desde: ".$this->email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
$this->response_status = 1;
$this->response_html = '<p>Mensaje enviado.Gracias por contactarnos!</p>';
}else{
$this->response_status = 0;
$this->response_html = '<p>Disculpe,no se pudo enviar su mensaje</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, $email_admin, $message_min_length);
$contact_form->sendRequest();
?>
Valora esta pregunta


0