
<?php $hoge = $_POST['hoge']; $obj=array(); $obj["hoge"]="urlエンコードされた文字::".urlencode($hoge)."通常文字::".$hoge; header("Content-Type: application/json; charset=utf-8"); echo json_encode($obj); ?>
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>zip358.com:sample-非同期2</title> <script src="//code.jquery.com/jquery-1.11.2.min.js"></script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script> $(function(){ $("#textarea-hoge").keyup(function() { $.ajax({ type: "POST", url: "sample-0722.php", dataType:'json', data: { "hoge" : $("#textarea-hoge").val() }, success: function(data) { $("#hoge2").text(data.hoge); }, error:function(data) { } }); }); }); </script> </head> <body> <br><br>文字をURLエンコード処理して返します。何か入力してください。<br>※非同期通信なので性格に処理してくれるわけではありません。<br><br> <textarea id="textarea-hoge" rows="10" cols="100"></textarea> <br> <div id="hoge2"></div> </body> </html>