• 회원가입
  • 로그인
  • 구글아이디로 로그인

[JSON] JS - JSON - HTML (= 테이블 및 드롭다운 형태로 출력 + 출력개수선택)

JSON은 JS로 쉽게 변환 가능.

JS는 웹페이지에서 HTML 만드는 데 사용 가능. 

 

 

[참고] a.php 세팅 - 아래 좌표 맨 하단 참고

https://homzzang.com/b/js-97?sca=JSON

 

 

HTML Table

 

JSON으로 받은 데이터로 HTML Table 만들기


/b.php 소스 


<p id="demo"></p>


<script>

var obj, dbParam, xmlhttp, myObj, x, txt = "";

obj = { limit: 20 };

dbParam = JSON.stringify(obj);

xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {

  if (this.readyState == 4 && this.status == 200) {

    myObj = JSON.parse(this.responseText);

    txt += "<table border='1'>"

    for (x in myObj) {

      txt += "<tr><td>" + myObj[x].mb_name + "</td></tr>";

    }

    txt += "</table>"    

    document.getElementById("demo").innerHTML = txt;

  }

};

xmlhttp.open("POST", "a.php", true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.send("x=" + dbParam);

</script>

 

 

 

 

HTML 동적 Table - 출력개수 선택

 

/b.php 소스 


<select id="limit_select" onchange="hz(this.value)">

  <option value="">출력 개수</option>

  <option value=1>1</option>

  <option value=2>2</option>

  <option value=3>3</option>

</select>


<p id="demo"></p>


<script>

function hz(sel) {

  var obj, dbParam, xmlhttp, myObj, x, txt = "";

  obj = { limit:sel };

  dbParam = JSON.stringify(obj);

  xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {

    if (this.readyState == 4 && this.status == 200) {

      myObj = JSON.parse(this.responseText);

      txt += "<table border='1'>"

      for (x in myObj) {

        txt += "<tr><td>" + myObj[x].mb_name + "</td></tr>";

      }

      txt += "</table>" 

      document.getElementById("demo").innerHTML = txt;

    }

  };

  xmlhttp.open("POST", "a.php", true);

  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  xmlhttp.send("x=" + dbParam);

}

</script>

 

 

 

 

HTML DropDown List - 특정 데이터 선택 메뉴

 

/b.php 소스 

 

<p id="demo"></p>


<script>

var obj, dbParam, xmlhttp, myObj, x, txt = "";

obj = { limit: 20 };

dbParam = JSON.stringify(obj);

xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {

  if (this.readyState == 4 && this.status == 200) {

    myObj = JSON.parse(this.responseText);

    txt += "<select>"

    for (x in myObj) {

      txt += "<option>" + myObj[x].mb_name;

    }

    txt += "</select>"

    document.getElementById("demo").innerHTML = txt;

  }

};

xmlhttp.open("POST", "a.php", true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.send("x=" + dbParam);

</script>

 



분류 제목
Basic JS - Common Mistakes -
Basic JS - Performance -
Basic JS - Reserved Words -
Basic JS - Versions -
Basic JS - JSON (제이슨) - 데이터 전송 위한 가벼운 자료 형식.
Form JS - Forms - 폼양식 유효성 제어
Form JS - Forms API - 폼유효성
Object JS - Object - 객체개념(=객체의미=객체정의) ★ 3
Object JS - Object Property - 객체속성 ★
Object JS - Object Methods - 객체메서드 ★
Object JS - Object Accessors - 객체접근자 (Getter/Setter = 게러/세러 = 게터/세터… 2
Functions JS - Function Definition - 함수선언방법 + 함수호출방법 ★★★ (= 함수구문 + 함수특…
Functions JS - Function Parameter/argument - 함수 (매개변수/독립변수) ※ 변수 종류 ※ …
Functions JS - Function Invocation - 함수호출방법1 = (함수방식 + 메서드방식 + 함수생성자방…
Functions JS - call() 메서드 - 함수호출방법2 (= call메서드 = 콜메서드)
DOM JS - DOM (= 돔 = 문서객체모델) 정의
DOM JS - Method - 메서드 (= HTML 요소에 대한 수행 작업)
DOM JS - Document - 문서객체
DOM JS - Element Selector - 주요 요소선택자 (= 객체찾기) ※ JS외부링크호출 주의사항
DOM JS - HTML - 내용입력/내용변경/속성값변경(=속성값입력)
3/67
목록
찾아주셔서 감사합니다. Since 2012