PHP & Others

jquery ajax php 예제

페이지 정보

본문

<div>

내용 : <span id="content_1">없음</span>

<button type="button" id="btn_1" value="1 class="btn">클릭</button>

</div>



<script>

$(".btn").click(function(){


  var i = $(this).val();


  $.ajax({ 

            url: 'http://test.com/ajax_result.php',

            data: {'seq_id': i}, 

            dataType: 'json',  /// json으로 결과를 받음 

            //processData: false,  //DOM 또는 urlencode 처리되지 않은 데이터를 보낼때 false 설정 //기본값은 true

            type: 'POST', 

            success: function (data) { 

         

                if (data.result === "success") { 

                    $('#content_'+i).text(data.content); 


                } else { 

                    console.log("data error"); 

                    alert('데이터 오류입니다');

                } 

                

            },

            error: function(xhr, status, error) {

                //alert(xhr.responseText);

                console.log(xhr.responseText);

            }

        }); 

});


</script>




ajax_result.php


<?php

.

.

.

$sql = " select content from table where seq_id='$seq_id' ";

.

.

.

if(...)

$result = 'success';

else

$result = 'fail';

.

.

.



$json = array('result'=>$result, 'content'=>$content);


echo json_encode($json);

?>

관련자료

등록된 댓글이 없습니다.
Today's proverb
전반전이 끝난 상황이라면 절대로 중단하지 말라. 전반전 점수는 큰 의미가 없다. 그 너머를 보라. 승리할 수 있는 길은 끝까지 인내하는 것이다.