2012-05-14 3 views
1

아약스를 처음 사용했습니다. 나는 단순히 ajax를 사용하여 이메일을 보내는 PHP 처리 스크립트에 변수를 전달하기를 원한다. 그러나, 나는 그것이 성공에도 불구하고, 그것은 성공했다 말을 아약스로 PHP 스크립트에서 다시 전송 아무것도 못하고, 그리고 난이 0 또는 1내 스크립트가 내 ajax 함수에 값을 반환하지 않는 이유는 무엇입니까?

내 아약스를 반환하도록 요청하는 것입니다 :

jQuery.ajax({ 
     //this is the php file that processes the data and send mail 
     url: "process.php", 

     //GET method is used 
     type: "GET", 

     //pass the data   
     data: dataString,  

     //Do not cache the page 
     cache: false, 

     //success 
     success: function (html) {  

     alert(html); 

      //if process.php returned 1/true (send mail success) 
      if (html==1) {     
       //hide the form 
       jQuery('.form').fadeOut('slow');     

       //show the success message 
       jQuery('.done').fadeIn('slow'); 

      //if process.php returned 0/false (send mail failed) 
      } else alert('Sorry, unexpected error. Please try again later.');    
     }  
     }); 

내 PHP는 대신 return

$username=htmlspecialchars(stripslashes($_GET['username'])); 
    $telnumber=htmlspecialchars(stripslashes($_GET['telnumber'])); 
    $email=htmlspecialchars(stripslashes($_GET['email'])); 
    $numberparty=htmlspecialchars(stripslashes($_GET['numberparty'])); 
    $message=htmlspecialchars(stripslashes($_GET['message'])); 
    $to=//myemail address; 

    $workshopName=htmlspecialchars(stripslashes($_GET['workshopName'])); 

    $subject='Booking request for: '.$workshopName; 

    $body= "<ul><li>Name: ".$username."</li>"; 
    $body.="<li>Email: ".$email."</li>"; 
    $body.="<li>Telephone Number: ".$telnumber."</li>"; 
    $body.="<li>Number in party: ".$numberparty."</li>"; 

    if(!empty($message)){ 
     $body.="<li>Message: ".$message."</li>"; 
    } 
    $body.="</ul>"; 

    function sendmail($to, $subject, $message, $from) { 
      $headers = "MIME-Version: 1.0" . "\r\n"; 
      $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
      $headers .= 'From: ' . $from . "\r\n"; 

      $result = mail($to,$subject,$message,$headers); 


      if ($result) return 1; 
      else return 0; 
    } 


    $result = sendmail($to, $subject, $body, $email); 

답변

4

, 스크립트로 다시 얻을 수 echo를 사용

if ($result) echo 1; 
else echo 0; 
당신이 단축 될 수 있습니다

도 좋아 : 당신은 AJAX를 사용하지 return 않는 경우

echo $result ? 1 : 0; 
+0

+1 8 초. –

+0

@Rocket : 그냥 커피를 가지고 있습니다 :) – Sarfraz

+0

여기에 질문에 조금 덧붙여서 아약스 함수에 반환 할 메시지를 추가 할 수 있습니까? 예를 들어 1 또는 0을 반환하고 0이면 $ string으로 문자열을 반환합니다. ? – Nicola

3

. 값이 echo이어야합니다.

if ($result) echo 1; 
else echo 0; 
관련 문제