나는 형태가 처음이지만 뭔가 간단한 것을 놓치고있는 것처럼 느껴집니다. '오류'에 대한 경고가 아닌 '성공'이 없습니다.AJAX/JSON/PHP를 사용하여 jquery 이메일 양식을 만드시겠습니까?
다음은 index.html 파일의 양식 코드입니다. 내의 .js에서
<form id="contact-form" name="contact-form" method="post">
<div id="formResponse"></div>
<label for="name">NAME:</label>
<input type="text" name="name" id="name" /><br/>
<label for="email">EMAIL:</label>
<input type="text" name="email" id="email" /><br/>
<label for="message">MESSAGE:</label>
<textarea name="message" rows="20" cols="20" id="message"></textarea><br/>
<input type="submit" name="submit" value="SEND" class="submit" />
</form>
Ajax 코드는
$('.submit').click(form_registration);
function form_registration() {
var input_data = $('#contact-form').serialize();
$.ajax({
type: 'post',
url: '../contact.php',
data: input_data,
dataType: 'json',
success: function(msg){
alert('SUCCESS!');
},
error: function(){
alert('ERROR!');
alert(input_data);
}
});
}
contact.php 파일
< ?php
if($_POST) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = trim($_POST['msg']);
<!-- //email address settings -->
$my_address = "MY EMAIL ADDRESS GOES HERE";
$headers = "From: ".$email;
$message = "Contact name: $name\nContact Email: $email\nContact Message: $msg";
$to = $my_address;
if ($name == "")
{
echo 'Name field is required';
<!-- $result = "Name field is required"; -->
}
else if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email))
{
echo 'Enter a valid email address';
<!-- $result = "Enter a valid email address"; -->
}
else if (strlen($msg) < 10)
{
echo 'Write more than 10 characters';
<!-- $result = "Write more than 10 characters"; -->
}
else
{
mail($to, $subject, $message, $headers);
echo "Your mail has been sent succesfully!";
}
if ($javascript_enabled == "true") {
echo 'javascript enabled';
die();
}
}
?>
다음
EXAMPLE의 파일. 하단의 연락처 링크를 클릭하십시오. 어떤 도움을 주셔서 감사합니다!