2011-02-17 4 views
0

안녕 얘들 아, 나에게 몇 가지 정보를 메일 간단한 PHP 파일을 만드는 방법을 알아.jquery : 보이지 않는 PHP 메일러를 호출 하시겠습니까? 아약스?

그러나 내가 모르는 것은 jquery로 php 파일을 호출하고 변수를 넘겨주는 방법입니다. 변수를 통해 넘겨 내가 JQuery와에서이 PHP 메일러를 호출하고 사용자로부터 HIDDEN 그렇게 할 수있는 방법isset()...

으로 작동 할 수 있습니다. 그래서 새로운 창을 팝업해서는 안되며 페이지 새로 고침이나 그와 같은 것이면 안됩니다.

$('a.report').click(function(e) { 
     e.preventDefault(); 
     var id = $(this).attr('href'); 

     //call mail script and pass along the "id" variable 

     //change text (maybe in a callback function IF THE MAILING WAS A SUCCESS. 
     $(this).parent().text('Thank you for reporting.'); 

    }) 

이메일 스크립트를 트리거해야하는 a.report 링크가 있습니다. 내 이메일 스크립트에서 jquery에 설정된 "id"변수에 액세스해야합니다. 그리고 php 스크립트가 콜백 함수를 사용하면 "보고 해 주셔서 감사합니다"라는 결과를 출력 할 수도 있습니다.

어떻게 수행하나요?

감사합니다.

답변

1

나는 $.post() 사용합니다 :

<script type='text/javascript'> 
$(function(){ 
    function onReportPosted(data) { 
     // data.status - either 'error' or 'success', from mailer.php 
     // data.message - some text, from mailer.php 
     $('.result').text(data.message); 
    } 
    $('a.report').click(function(e) { 
     $('.result').text('sending report...'); 
     var data = { 
      text: $('textarea[name=text]').val() 
     }; 
     $.post(
      'mailer.php', 
      data, 
      onReportPosted, 
      'json' 
     ); 
     return false; 
    }); 
}); 
</script> 

그리고 mailer.php에서을 :

<?php 
    if (isset($_POST['text'])) { 
     // mail()... 
     $result = array(
      'status' => 'success', 
      'message' => 'thank you for reporting', 
     ); 
    } else { 
     $result = array(
      'status' => 'error', 
      'message' => 'some error occurred', 
     ); 
    } 
    header('Content-Type: application/json'); 
    echo json_encode($result); 
    exit; 

업데이트 :

<script type='text/javascript'> 
    $(function(){ 
     $('a.report').click(function(){ 
      var htmlElement = $(this).parent(); 
      var data = { 
       // ... 
      }; 
      $.post(
       document.location.toString(), 
       data, 
       function(data) { 
        htmlElement.html(data.message); 
       }, 
       'json' 
      ); 
      return false; 
     }); 
    }); 
</script> 
: 여기에 특정 요소에 콜백 "타이"하는 방법을 방법이야
+0

.result div에 메시지를 넣고 싶지는 않지만 mailer.php를 트리거 한 링크를 교체하십시오. 클릭하면 링크 텍스트를 ... (3 도트)로 바꿉니다. mailer.php의 응답이 돌아 왔을 때 전체 부모를 "보고 해 주셔서 감사합니다"라는 메시지로 바꿉니다. – matt

+1

@mathiregister, 업데이트 된 게시물보기. – binaryLV

+0

또 다른 방법은'mailer.php'에 전달 된 ID를 가지고 돌아 오는 것입니다. ID는 DB의 특정 행과 HTML의 특정 요소를 가리 킵니다. 그런 다음'onReportPosted (data)'에서'$ ('item_'+ data.id). 텍스트 (data.message)와 같은 것을 할 수 있습니다. – binaryLV

0

는 호출하고 PHP 스크립트로 ID를 전달하는 방법을 알고 $ .post 참조 (http://api.jquery.com/jQuery.post/)

이 될 것입니다 뭔가를해야만,

$.ajax({ 
    context: $(this), 
    type: 'POST', 
    url: 'script.php', 
    data: {id: id }, 
    success: function() { 
    $(this).parent().text('Thank you for reporting.'); 
    } 
}); 

그리고 PHP 스크립트에서

같은 $id = $_POST['id'];

0

시도 : PHP에서

$.post('script.php', {variableName: value, variable2Name: value2}); 

: $value = $_REQUEST['variableName']; $value2 = $_REQUEST['variable2Name'];

0

jQuer y는 AJAX API catagory하에 우리에게 훌륭한 AJAX 메소드를 제공합니다. 나는 $.post이 당신이 찾고있는 것이라고 생각합니다. 내가 PHP에서 구문 분석 방법

$.ajax({ 
type: 'POST', 
url: 'http://path/to/script.php', 
data: JSON.stringify({key: 'value', array: ['one', 'two', 'three']), 
dataType: 'json', 
success: function(data){ 
    console.log('Mailer returned with object ', JSON.parse(data)); 
} 
}); 

을 그리고 여기에 : : 이것은 내 Grooveshark에의 userscript에 간단한 PHP 스크립트로 JSON 데이터를 전송하는 방법입니다

$incoming = json_decode(file_get_contents("php://input"), true); 

이것은 JSON 콘텐츠를 중첩 연관 배열을 반환 아주 구문 분석하기 쉽습니다! JSON을 데이터 교환 형식으로 사용하는 것이 좋습니다. XML보다 훨씬 낫다.

관련 문제