2015-01-26 4 views
0

내 웹 사이트에 대한 문의 양식을 작성하고 싶습니다.이 글자는 blog-post이지만 더 간단합니다 (이름, 주소, 메시지가 필요함).문의 양식이 작동하지 않습니다 (PHP 쪽)

내 컴퓨터에서 로컬 호스트의 파일을 호스팅하여 PHP를 시험해 봅니다. 나는 PHP 코드에 오류가 있다고 생각하지만 그것을 찾을 수 없었다.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

    <script type="text/javascript"> 
    $(document).ready(function() { 
    $("#submit_btn").click(function() { 
     var proceed = true; 
     //simple validation at client's end 
     //loop through each field and we simply change border color to red for invalid fields  
     $("#contact_form input[required=true], #contact_form textarea[required=true]").each(function(){ 
      $(this).css('border-color',''); 
      if(!$.trim($(this).val())){ //if this field is empty 
       $(this).css('border-color','red'); //change border color to red 
       proceed = false; //set do not proceed flag 
      } 
      //check invalid email 
      var email_reg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
      if($(this).attr("type")=="email" && !email_reg.test($.trim($(this).val()))){ 
       $(this).css('border-color','red'); //change border color to red 
       proceed = false; //set do not proceed flag    
      } 
     }); 

     if(proceed) //everything looks good! proceed... 
     { 
      //get input field values data to be sent to server 
      post_data = { 
       'user_name'  : $('input[name=name]').val(), 
       'user_email' : $('input[name=email]').val(), 
       'msg'   : $('textarea[name=message]').val() 
      }; 

      console.log(post_data) 
      //Ajax post data to server 
      $.post('php/contact_me.php', post_data, function(response){ 
       console.log(response) 
       if(response.type == 'error'){ //load json data from server and output message  
        output = '<div class="error">'+response.text+'</div>'; 
       }else{ 
        // output = '<div class="success">'+response.text+'</div>'; 
        //reset values in all input fields 
        // $("#contact_form input[required=true], #contact_form textarea[required=true]").val(''); 
        // $("#contact_form #contact_body").slideUp(); //hide form after success 
       } 
       // $("#contact_form #contact_results").hide().html(output).slideDown(); 
      }, 'json'); 
     } 
    }); 

PHP 파일 :

<?php 
if($_POST) 
{ 
    $to_email  = "[email protected]"; //Recipient email, Replace with own email here 

    //check if its an ajax request, exit if not 
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 

     $output = json_encode(array(//create JSON data 
      'type'=>'error', 
      'text' => 'Sorry Request must be Ajax POST' 
     )); 
     die($output); //exit script outputting json data 
    } 

    //Sanitize input data using PHP filter_var(). 
    $user_name  = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING); 
    $user_email  = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL); 
    $message  = filter_var($_POST["msg"], FILTER_SANITIZE_STRING); 

    //additional php validation 
    if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error. 
     $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!')); 
     die($output); 
    } 
    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation 
     $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!')); 
     die($output); 
    } 
    if(strlen($message)<3){ //check emtpy message 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.')); 
     die($output); 
    } 

    //email body 
    $message_body = $message."\r\n\r\n-".$user_name."\r\nEmail : ".$user_email ; 

    //proceed with PHP email. 
    $headers = 'From: '.$user_name.'' . "\r\n" . 
    'Reply-To: '.$user_email.'' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

    $send_mail = mail($to_email, $subject, $message_body, $headers); 

    if(!$send_mail) 
    { 
     //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens) 
     $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail!')); 
     die($output); 
    }else{ 
     $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email')); 
     die($output); 
    } 
} 
?> 
+0

로컬 웹 서버를 실행하고 있습니까? localhost에서 PHP를 실행하려면 XAMPP 또는 이와 유사한 명령을 실행해야합니다. –

+0

예, 있습니다. 나는 문제가 php 파일에 있다고 생각한다. $ .post ('php/contact_me.php'..) 호출 전에 데이터를 인쇄하려고하면 성공적으로 그 일을 할 수있다. 하지만 호출 후 아무 것도 볼 수 없습니다 – user3378649

+0

단위 테스트에 대한 생각 –

답변

-1

당신은 실제로 메일을 보낼 수있는 메일 서버가 있어야합니다. http://www.postfix.org/ 또는 http://www.pmail.com/과 같은 것을 찾을 수 있습니다. 실제로 설정을 마치면 메일을 보낼 수 있습니다.

+0

어떻게하면됩니까? 나는 wamp를 사용하고있다. – user3378649

관련 문제