2012-05-16 3 views
0

나는 형태가 처음이지만 뭔가 간단한 것을 놓치고있는 것처럼 느껴집니다. '오류'에 대한 경고가 아닌 '성공'이 없습니다.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의 파일. 하단의 연락처 링크를 클릭하십시오. 어떤 도움을 주셔서 감사합니다!

답변

0

좋아, 먼저이

초를 제출에서 form을 방지하기 위해 닫는 대괄호 } 이전 form_registration()return false에 추가해야, 당신의 contact.php 유효한 개방 PHP 태그 변경 < ?php<?php

할 필요가 없습니다 그것을 내놔.

2

이 코드를 사용해보십시오. 코드에 몇 가지 변경을 가하고 이제는 완벽하게 작동합니다.

Contact.html

<head> 
    <script type="text/javascript" src="js/jquery.js"></script> 
</head> 
<body> 
    <form id="contact" action="contact.php" name="contact" 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="sub">SUBJECT</label> 
<input type="sub" name="sub" id="subject" /><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> 
</body> 

Contact.php

<?php 
if($_POST) { 

    $name = trim($_POST['name']); 
    $email = trim($_POST['email']); 
    $msg = trim($_POST['message']); 
    $subject = trim($_POST['sub']); 
    //email address settings 
    $my_address = "[email protected]"; 
    $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($subject) == "") 
    { 
     echo 'Please Write Subject'; 
     $result = "Please Write Subject"; 
     } 
     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!"; 
    } 
} 
?> 

Contact.js

$('.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); 
     } 
}); 
return false; 
} 
관련 문제