2015-06-06 27 views
11

PHP/jquery 'da yeni yepyeni bir json biçiminde ajax gibi bir form alanından (isim, yaş, vb) json verilerini nasıl göndereceğinizi öğrenmek istiyorum. Ne yazık ki bu konuyla ilgili herhangi bir bilgi bulamıyorum, bunu dinamik olarak yapmak bile mümkün mü? Google aramaları yalnızca verileri manuel olarak oluşturmak gibi geri tepkiler verir. gibi: name: X Y, age: 32, vb.jquery ile form verilerini gönderin ajax json

Bunu yapmak için yine var mı?

Yardımın için teşekkürler!

Düzenleme:

<form action="test.php" method="post"> 
Name: <input type="text" name="name"><br> 
Age: <input type="text" name="email"><br> 
FavColor: <input type="text" name="favc"><br> 
<input type="submit"> 
</form> 
+0

Lütfen daha önce çalıştığınız bazı kodları gösteriniz –

+0

Merhaba Oli Soproni B.! Yorumunuz için teşekkürler, soru form kodu ile düzenlenir. – user1888798

cevap

14

basit burada bir

olduğunu test için benim test.php sadece

<?php 

// this is just a test 
//send back to the ajax request the request 

echo json_encode($_POST); 
olduğunu

buraya sunucusu (php) için formfields veri gönderme aynı dizinde

3

Böyle serialize() kullanabilirsiniz:

burada
$.ajax({ 
    cache: false, 
    url: 'test.php', 
    data: $('form').serialize(), 
    datatype: 'json', 
    success: function(data) { 

    } 
}); 
+0

Eğer '$ (' form '). Serialize() 'kullanıyorsanız, o zaman veriyi bir nesneyi oluşturmazsınız; sadece "data" olmalı: $ ('form'). serialize(), '. –

1

yılında yer hangi genellikle geri bulunabilir POST yöntemiyle yapılır vardır benim index.html

<!DOCTYPE html> 
<html> 

<head> 

</head> 
<body> 

<form id="form" action="" method="post"> 
Name: <input type="text" name="name"><br> 
Age: <input type="text" name="email"><br> 
FavColor: <input type="text" name="favc"><br> 
<input id="submit" type="button" name="submit" value="submit"> 
</form> 




<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<script> 
    $(document).ready(function(){ 
     // click on button submit 
     $("#submit").on('click', function(){ 
      // send ajax 
      $.ajax({ 
       url: 'test.php', // url where to submit the request 
       type : "POST", // type of action POST || GET 
       dataType : 'json', // data type 
       data : $("#form").serialize(), // post data || get data 
       success : function(result) { 
        // you can see the result from the console 
        // tab of the developer tools 
        console.log(result); 
       }, 
       error: function(xhr, resp, text) { 
        console.log(xhr, resp, text); 
       } 
      }) 
     }); 
    }); 

</script> 
</body> 
</html> 

İkisi dosyasıdır PHP içinde süper küresel dizi $ _POST. Sunucuya göndermeden önce JSON'a dönüştürmenize gerek yoktur. Küçük örnek:

<?php 

if($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    echo '<pre>'; 
    print_r($_POST); 
} 
?> 
<form action="" method="post"> 
<input type="text" name="email" value="[email protected]" /> 
<button type="submit">Send!</button> 

AJAX ile aynı şeyi yalnızca sayfa yenileme olmadan yapabilirsiniz.