2013-05-17 4 views
0

변수 $pos에는 내가 누른 제출 버튼 이후에 값이 없습니다.Ajax가 GET을 사용하여 PHP에 변수를 넘김

<?php 
    $pos = $_GET['pos']; 
    echo "<br><br>pos = ".$pos; 
?> 

<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=us-ascii"> 
    <title></title> 
    <script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'></script> 

    <script type='text/javascript'> 

     $(window).load(function(){ 

      var position = 128; 

      $('#submit').click(function(){ 
        $.ajax({ 
         type:"GET", 
         url: "ajax_test.php", 
         data: { pos : position }, 
         success: function(){ 
         //do stuff after the AJAX calls successfully completes 
        } 
       }); 
      }); 
    }); 
    </script> 

</head> 

<body> 
    <br> 
    <button id="submit">Submit</button> 
</body> 
</html> 

FF의 웹 콘솔을 보여줍니다

[12:41:55.027] GET http://somehost.com/ajax_test.php?pos=128 [HTTP/1.1 200 OK 187ms] 
+0

# 제출 유형의 입력이 제출되고, 예를 들어 appendTo()와 jQuery를 사용하여 데이터를 삽입 할 수 있습니다, 맞습니까? 그리고 그것은 양식의 내부에 위치합니다, 맞습니까? –

+0

$ pos가 정의되지 않았습니다. 정의 된 위치 만 –

+0

HTTP 프로토콜, 요청 및 응답 및 사용 방법에 대해 알아야합니다. 모든 것이 곧바로 진행됩니다. – moonwave99

답변

3
<?php 
    if($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['ajax'])){ 
    $pos = $_GET['pos']; 
    echo $pos; exit; 
    } 
?> 

<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=us-ascii"> 
    <title></title> 
    <script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'></script> 

    <script type='text/javascript'> 

     $(window).load(function(){ 

      var position = 128; 

      $('#submit').click(function(){ 
        $.ajax({ 
         type:"GET", 
         url: "test.php", 
         data: { pos : position , ajax:1}, 
         success: function(response){ 
         document.getElementById('response').innerHTML ='pos='+response 
         //do stuff after the AJAX calls successfully completes 
        } 
       }); 
      }); 
    }); 
    </script> 

</head> 

<body> 
    <br> 
    <div id="response">pos=</div> 
    <button id="submit">Submit</button> 
</body> 
</html> 
+0

이 덕분에 많은 감사를드립니다! – Oualid

+0

왜'$ ('# someID')'와'document.getElementById()'가 같은 코드에 섞여 있습니까? – moonwave99

1

귀하의 코드가 정확한지, 당신은 단지 GET 요청하여 아약스의 결과를 사용하지. 당신이 당신의 자바 스크립트 코드에서 PHP 스크립트를 호출하는 것을 제외하고

success: function(data) { 
    alert(data); 
} 
1

난 당신의 코드에서 오류가 표시되지 않으며, 출력에 아무것도하지 않습니다

성공 기능이 시도 . echo $ pos 출력은 아무 곳에도 사용되지 않습니다. 스크립트에 다음을 추가하십시오.

success: function(result){ 
    $('#YourResultDiv').html(result); 
} 
0

AJAX 호출에서 변수를 검색 할 수 없습니다.

Ajax는 비동기이며 이미 계산 된 PHP 코드를 수정할 수 없습니다.

하지만

관련 문제