ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JQuery]AJAX/PHP/POST - EXAMPLE
    카테고리 없음 2013. 7. 4. 18:00

    JQuery&AJAX. PHP/POST EXAMPLE

     index.html

    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
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    <!DOCTYPE html>
    <html lang="kr">
    <head>
        <meta charset="utf-8" />
        <title>jQuery & Ajax & PHP Example</title>
         
        <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
        <script>
        $(document).ready(function()
        {
             
            $('#send').click(function()
            {
                // var data = {파라메터명 : 값};
                var data = {request : $('#request').val()};
     
                /**
                 * @param type  : HTTP
                 * @param url   : 리퀘스트 보낼 URL
                 * @param data  : 보낼 데이터
                 */
                $.ajax({
                    type: "POST",
                    url: "send.php",
                    data: data,
                    /**
                     * Ajax통신이 끝났을 경우 불러올 메서드
                     */
                    success: function(data, dataType)
                    {
                        //Ajax송신이 성공했을 경우 불러옴
     
                        //PHP로부터 레스폰스받은 데이터
                        alert(data);
                    },
                    /**
                     * Ajax가 실패한 경우
                     */
                    error: function(XMLHttpRequest, textStatus, errorThrown)
                    {
                        alert('Error : ' + errorThrown);
                    }
                });
                 
                //submit후에 페이지의 리로드를 막음.
                return false;
            });
        });
        </script>
    </head>
    <body>
        <h1>jQuery & Ajax & PHP Example</h1>
        <form method="post">
            <p><textarea id="request" cols="20" rows="4"></textarea></p>
            <p><input id="send" value="보내기" type="submit" /></p>
        </form>
    </body>
    </html>


    send.php


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    header("Content-type: text/plain; charset=UTF-8");
    if (isset($_POST['request']))
    {
        echo "OK";
    }
    else
    {
        echo 'The parameter of "request" is not found.';
    }
    ?>



Designed by Tistory.