2014-12-26 4 views
0

Я пытаюсь запустить очень простой PHP-код через AJAX и получить данные с PHP-страницы в AJAX-успех.PHP-данные в AJAX-успехе?

Однако, я не получаю ничего в успехе AJAX со страницы PHP и плохо его прослушиваю.

это код 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

add dataType: '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

Смежные вопросы