2014-12-26 3 views
0

저는 AJAX를 통해 매우 기본적인 PHP 코드를 실행하고 PHP 페이지에서 AJAX 성공으로 데이터를 다시 가져 오려고합니다.AJAX에서 성공한 PHP 데이터?

그러나 PHP 페이지에서 AJAX 성공과 나쁘게 도청 한 점은 없습니다.

이는 AJAX 코드 :

$(document).ready(function(){ 
$(function(){ 
    $('#form-post').on('submit', function(e){ 

     // prevent native form submission here 
     e.preventDefault(); 

     // now do whatever you want here 
     $.ajax({ 
      type: $(this).attr('method'), // <-- get method of form 
      url: $(this).attr('action'), // <-- get action of form 
      data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file 
      beforeSend: function(){ 
       //$('#result').html('<img src="loading.gif" />'); 
      }, 
      success: function(data){ 
       $('#messageme').html(data); 
      } 
     }); 
    }); 
}); 
}); 

이는 형태 :

<form id="form-post" action="post-code.php" method="post" > 
<input type="hidden" value="Post" name="submit" /> 
<input type="text" class="inp-form" name="postcode" id="postcode" placeholder="Enter Post Code " /><br /><br /><input type="text" id="messageme" /><br /><br /> 
<input id="findAd" type="button" value=" Search For Address" /> 
</form> 

매우 간단한 PHP :

<?php 

$street = "some"; 
    echo $street; 
?> 

사람이에 대한 조언을주십시오 수 있을까?

+4

이 $ ('# messageme')을 시도하십시오 .val (data); – AVM

+0

데이터 형식 추가 : 'html' –

+0

@AVM, 아니요, 불행히도 나는 아무것도 얻지 못합니다! – william

답변

4

시도해주세요. type="button"type="submit"으로 변경하십시오.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Untitled Document</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
</head> 

<body> 
<form id="form-post" action="post-code.php" method="post" > 
<input type="hidden" value="Post" name="submit" /> 
<input type="text" class="inp-form" name="postcode" id="postcode" placeholder="Enter Post Code " /><br /><br /> 
<input type="text" id="messageme" /><br /><br /> 
<input id="findAd" type="submit" value=" Search For Address" /> 
</form> 
<script>$(document).ready(function(){ 

$(function(){ 
$('#form-post').on('submit', function(e){ 

    // prevent native form submission here 
    e.preventDefault(); 

    // now do whatever you want here 
    $.ajax({ 
     type: $(this).attr('method'), // <-- get method of form 
     url: $(this).attr('action'), // <-- get action of form 
     data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file 
     beforeSend: function(){ 
      //$('#result').html('<img src="loading.gif" />'); 
     }, 
     success: function(data){ 
      alert(data); 
      $('#messageme').html(data); 
     } 
     }); 
     }); 
     }); 
     }); 
    </script> 


    </body> 
    </html> 
+0

잠깐, 지금은 분명해 보인다 ...;) –

+0

@william : 안녕! 너에게 효과가 있니? – Priyank

+1

OMG, 나는 이것을 지난 2 시간 동안 알아 내려고 노력해 왔으며 그렇게 간단했습니다. 고마워. – william