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);

?>

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
행복해지고 싶다면, 잠시 동안만이라도 가슴에 손을 얹고 생각해 보라. 그러면 진정한 즐거움은, 발치에 돋아나는 잡초나 아침 햇살에 빛나는 꽃의 이술과 같이 우리 주변에 무수히 널려 있다는 것을 알 수 있을 것이다. 《하루 5분 생각이 인생을 결정한다 》 (이범준)