Ajaxで複数の値を返す

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQeury</title>
    <style>
    .myStyle{
      border:5px solid green;
      font-size:48px;
    }
    </style>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
      <p>jQuery</p>
 
      <p>
        <input type="text" name="name" id="name">
        <input type="button" id="greet" value="Greet!">
      </p>
      <div id="result"></div>
 
      <script>
        $(function(){
 
          $('#greet').click(function(){
 
            $.get('greet.php', {
              name: $('#name').val()
            }, function(data){
                $('#result').html(data.message + '(' + data.length + ')');
            });
          });
        });
      </script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
<?php
 
// echo htmlspecialchars("hi!" . $_GET&#91;"name"&#93;, ENT_QUOTES, "utf-8");
 
$rs = array(
  "message" => htmlspecialchars("hi!" . $_GET["name"], ENT_QUOTES, "utf-8"),
  "length" => strlen($_GET["name"])
);
 
header('Content-Type: application/json; charset=utf-8');
echo json_encode($rs);

%e7%84%a1%e9%a1%8c