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

[AJAX] JS - AJAX - DB 아작스 예제 ★ (= 데이터베이스 정보 가져오기) ※ select 태그 option 값 변경 시 ajax로 관련 값 호출


b.php (앞단) 

 

<style>

table,th,td {

border:1px solid silver;

border-collapse:collapse;

padding:10px;

}

</style>


<form action=""> 

  <select name="mb_id" onchange="hz(this.value)">

    <option value="">아이디 선택</option>

    <option value="aaa">aaa</option>

    <option value="bbb">bbb</option>

    <option value="ccc">ccc</option>

  </select>

</form>


<div id="txtHint">회원정보 출력위치</div>


<script>

function hz(str) {

  var xhttp;  

  if (str == "") {

    document.getElementById("txtHint").innerHTML = "";

    return;

  }

  xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function() {

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

      document.getElementById("txtHint").innerHTML = this.responseText;

    }

  };

  xhttp.open("GET", "a.php?q="+str, true);

  xhttp.send();

}

</script>

 


a.php (뒷단)

 

<?php

$mysqli = new mysqli("localhost", "root", "autoset", "mw7"); // DB 연결 (호스트,아이디,비번,DB명)

if($mysqli->connect_error) {

  exit('연결 실패');

}


$sql = "SELECT mb_id, mb_name, mb_level, mb_point FROM g5_member WHERE mb_id = ?";


$stmt = $mysqli->prepare($sql);

$stmt->bind_param("s", $_GET['q']);

$stmt->execute();

$stmt->store_result();

$stmt->bind_result($mb_id, $mb_name, $mb_level, $mb_point);

$stmt->fetch();

$stmt->close();


echo "<table>";

echo "<tr>";

echo "<th>아이디</th>";

echo "<th>닉네임</th>";

echo "<th>레벨</th>";

echo "<th>포인트</th>";

echo "</tr>";

echo "<tr>";

echo "<td>" . $mb_id . "</td>";

echo "<td>" . $mb_name . "</td>";

echo "<td>" . $mb_level . "</td>";

echo "<td>" . $mb_point . "</td>";

echo "</tr>";

echo "</table>";

?>


PS. 다른 예제: https://homzzang.com/b/php-56

 


분류 제목
String JS - toUpperCase() 메서드 - 대문자로 변환
String JS - trim() 메서드 ★ - 문자열양쪽 공백제거 (= trim메서드 = 트림 메서드)
String JS - valueOf() 메서드 - 객체값 (문자열자체 = 밸류어브)
String JS - anchor() 메서드 - name 속성 갖는 앵커태그 (= 링크태그) (비표준)
String JS - big() 메서드 - 큰글씨 (= 큰글자 = 글자 크게) (비표준)
String JS - blink() 메서드 - 글자 깜빡임. (비표준)
String JS - bold() 메서드 - 굵은글씨 (= 글자 굵게) (비표준)
String JS - fixed() 메서드 - 텔레타이프 텍스트 (비표준)
String JS - fontcolor() 메서드 - 글자색깔 (비표준)
String JS - fontsize() 메서드 - 글자크기 (비표준)
String JS - italics() 메서드 - 이탤릭체 (비표준)
String JS - link() 메서드 - src 속성 갖는 앵커태그 (= 링크태그) (비표준)
String JS - small() 메서드 - 작은글씨 (= 글자 작게) (비표준)
String JS - strike() 메서드 - 취소선 (= strike메서드 = 스트라이크메서드, HTML5제외)
String JS - sub() 메서드 - 아래첨자 (비표준) (= sub메서드 = 서브메서드)
String JS - sup() 메서드 - 위첨자 (비표준)
Number JS - Number -
Number JS - constructor - 객체생성자함수 (숫자 경우)
Number JS - MAX_VALUE - JS최대값 (= JS에서 가장큰수)
Number JS - MIN_VALUE - JS최소값 (= JS가장작은값)
7/67
목록
찾아주셔서 감사합니다. Since 2012