Bonjour aux membre de ce forum !
Mon pb : je ne vois pas où est l'erreur dans l'envoi du mail via ce code (formulaire html, fction mail en php et contrôle du formulaire en js).
Le form. en html :
Le php :
Le Js :
Lorsque je teste l'envoi via le site j'ai toujours l'erreur : could not complete your request. sorry!
Désolé pour la longueur du post mais je craque
Mes connaissances : pas mal en html et css, mais quasi zéro en php et js.
Merci pour vos retours.
Modifié par 6l20 (12 Jun 2013 - 11:57)
Mon pb : je ne vois pas où est l'erreur dans l'envoi du mail via ce code (formulaire html, fction mail en php et contrôle du formulaire en js).
Le form. en html :
<fieldset id="contact_form">
<div id="msgs"> </div>
<form id="cform" name="cform" method="post" action="contact.php">
<input type="text" id="name" name="name" value="Full Name*" onfocus="if(this.value == 'Full Name*') this.value = ''"
onblur="if(this.value == '') this.value = 'Full Name*'" />
<input type="text" id="email" name="email" value="Email Address*" onfocus="if(this.value == 'Email Address*') this.value = ''"
onblur="if(this.value == '') this.value = 'Email Address*'" />
<input type="text" id="subject" name="subject" value="Subject*" onfocus="if(this.value == 'Subject*') this.value = ''"
onblur="if(this.value == '') this.value = 'Subject*'" />
<textarea id="msg" name="msg" onfocus="if(this.value == 'Your Message*') this.value = ''"
onblur="if(this.value == '') this.value = 'Your Message*'">Your Message*</textarea>
<button id="submit" class="button"> Send Message</button>
</form>
</fieldset>
Le php :
<?php
$send_email_to = "monmail@ouf.fr";
function send_email($name,$email,$subject,$message)
{
global $send_email_to;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$email. "\r\n";
$message = "<strong>Email = </strong>".$email."<br>";
$message .= "<strong>Name = </strong>".$name."<br>";
$message .= "<strong>Message = </strong>".$email_message."<br>";
@mail($send_email_to, $subject, $message,$headers);
return true;
}
function validate($name,$email,$message,$subject)
{
$return_array = array();
$return_array['success'] = '1';
$return_array['name_msg'] = '';
$return_array['email_msg'] = '';
$return_array['message_msg'] = '';
$return_array['subject'] = '';
if($email == '')
{
$return_array['success'] = '0';
$return_array['email_msg'] = 'email is required';
}
else
{
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$return_array['success'] = '0';
$return_array['email_msg'] = 'enter valid email.';
}
}
if($name == '')
{
$return_array['success'] = '0';
$return_array['name_msg'] = 'name is required';
}
else
{
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$return_array['success'] = '0';
$return_array['name_msg'] = 'enter valid name.';
}
}
if($subject == '')
{
$return_array['success'] = '0';
$return_array['subject_msg'] = 'subject is required';
}
if($message == '')
{
$return_array['success'] = '0';
$return_array['message_msg'] = 'message is required';
}
else
{
if (strlen($message) < 2) {
$return_array['success'] = '0';
$return_array['message_msg'] = 'enter valid message.';
}
}
return $return_array;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$return_array = validate($name,$email,$message,$subject);
if($return_array['success'] == '1')
{
send_email($name,$email,$subject,$message);
}
header('Content-type: text/json');
echo json_encode($return_array);
die();
?>
Le Js :
//bind send message here
$('#submit').click(sendMessage);
});
/* Contact Form */
function checkEmail(email) {
var check = /^[\w\.\+-]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/;
if (!check.test(email)) {
return false;
}
return true;
}
function sendMessage() {
// receive the provided data
var name = $("input#name").val();
var email = $("input#email").val();
var subject = $("input#subject").val();
var msg = $("textarea#msg").val();
// check if all the fields are filled
if (name == '' || name == 'Full Name*' || email == '' || email == 'Email Address*' || subject == '' || subject == 'Subject*' || msg == '' || msg == 'Your Message*') {
$("div#msgs").html('<p class="warning">You must enter all the fields!</p>');
return false;
}
// verify the email address
if (!checkEmail(email)) {
$("div#msgs").html('<p class="warning">Please enter a valid Email Address</p>');
return false;
}
// make the AJAX request
var dataString = $('#cform').serialize();
$.ajax({
type: "POST",
url: 'contact.php',
data: dataString,
dataType: 'json',
success: function (data) {
if (data.success == 0) {
var errors = '<ul><li>';
if (data.name_msg != '')
errors += data.name_msg + '</li>';
if (data.email_msg != '')
errors += '<li>' + data.email_msg + '</li>';
if (data.message_msg != '')
errors += '<li>' + data.message_msg + '</li>';
if (data.subject_msg != '')
errors += '<li>' + data.subject_msg + '</li>';
$("div#msgs").html('<p class="error">Could not complete your request. See the errors below!</p>' + errors);
}
else if (data.success == 1) {
$("div#msgs").html('<p class="error">You message has been sent successfully!</p>');
}
},
error: function () {
$("div#msgs").html('<p class="error">Could not complete your request. Sorry!</p>');
}
});
return false;
}
Lorsque je teste l'envoi via le site j'ai toujours l'erreur : could not complete your request. sorry!
Désolé pour la longueur du post mais je craque
Mes connaissances : pas mal en html et css, mais quasi zéro en php et js.
Merci pour vos retours.
Modifié par 6l20 (12 Jun 2013 - 11:57)