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
진실한 의식을 갖춘 영혼은 자신보다 훨씬 뛰어난 무엇을 발견할 줄 압니다. 칭찬이란 이해입니다. 근본적으로 누구나 위대하고 훌륭합니다. 누군가를 아무리 칭찬한다 해도 지나치지 않습니다. 타인 속에 있는 위대함과 아름다움을 발견하는 눈을 기르십시오. 《보여줄 수 있는 사랑은 아주 작습니다》 (칼릴지브란)