Hướng dẫn php dynamic chart mysql - biểu đồ động php mysql

AJAX có thể được sử dụng để giao tiếp tương tác với cơ sở dữ liệu.

Ví dụ cơ sở dữ liệu AJAX

Ví dụ sau đây sẽ chứng minh làm thế nào một trang web có thể tìm nạp thông tin từ cơ sở dữ liệu với AJAX:

Thí dụ

Thông tin người sẽ được liệt kê ở đây ...

Ví dụ giải thích - Cơ sở dữ liệu MySQL

Bảng cơ sở dữ liệu chúng tôi sử dụng trong ví dụ trên có vẻ như thế này:

TôiHọHọTuổi tácQuê nhàNghề nghiệp
1 PeterGriffin41 QuahogNhà máy bia
2 LoisGriffin40 QuahogNhà máy bia
3 LoisNewport39 QuahogNhà máy bia
4 LoisNewport41 QuahogNhà máy bia

Lois

Newport

Giáo viên piano

Joseph

Thí dụ

Swanson


function showUser[str] {
  if [str == ""] {
    document.getElementById["txtHint"].innerHTML = "";
    return;
  } else {
    var xmlhttp = new XMLHttpRequest[];
    xmlhttp.onreadystatechange = function[] {
      if [this.readyState == 4 && this.status == 200] {
        document.getElementById["txtHint"].innerHTML = this.responseText;
      }
    };
    xmlhttp.open["GET","getuser.php?q="+str,true];
    xmlhttp.send[];
  }
}


Cảnh sát

  Select a person:
  Peter Griffin
  Lois Griffin
  Joseph Swanson
  Glenn Quagmire
 



Person info will be listed here...

GLenn

Quigmire

Phi công

Ví dụ giải thích

  • Trong ví dụ trên, khi người dùng chọn một người trong danh sách thả xuống ở trên, một hàm gọi là "showuser []" được thực thi.
  • Hàm được kích hoạt bởi sự kiện Onchange.
  • Đây là mã HTML:
  • Hàm showuser [str] {& nbsp; if [str == ""] {& nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ["txthint"]. & nbsp; } other {& nbsp; & nbsp; & nbsp; & nbsp; var xmlHttp = new xmlHttpRequest []; & nbsp; & nbsp; & nbsp; xmlHttp.onReadyStateChange = function [] {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; if [this.ReadyState == 4 && this.status == 200] {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ["txthint"]. Internhtml = this.responsetext; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;} thật]; & nbsp; & nbsp; & nbsp; xmlhttp.send []; & nbsp; }}

& nbsp; Chọn một người: & nbsp; Peter Griffin & NBSP; Lois Griffin & NBSP; Joseph Swanson & NBSP; Glenn Quagmire & NBSP; Thông tin người sẽ được liệt kê ở đây ...

The page on the server called by the JavaScript above is a PHP file called "getuser.php".

Chạy ví dụ »

Giải thích mã:



table {
  width: 100%;
  border-collapse: collapse;
}

Đầu tiên, kiểm tra xem người được chọn. Nếu không có ai được chọn [str == ""], hãy xóa nội dung của txthint và thoát chức năng. Nếu một người được chọn, hãy làm như sau:
  border: 1px solid black;
  padding: 5px;
}

Tạo đối tượng XMLHTTPREQUEST


GLenn
$q = intval[$_GET['q']];

Quigmire
if [!$con] {
  die['Could not connect: ' . mysqli_error[$con]];
}

Phi công
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query[$con,$sql];

Ví dụ giải thích

Firstname
Lastname
Age
Hometown
Job
";
while[$row = mysqli_fetch_array[$result]] {
  echo "";
  echo "" . $row['FirstName'] . "";
  echo "" . $row['LastName'] . "";
  echo "" . $row['Age'] . "";
  echo "" . $row['Hometown'] . "";
  echo "" . $row['Job'] . "";
  echo "";
}
echo "";
mysqli_close[$con];
?>

Trong ví dụ trên, khi người dùng chọn một người trong danh sách thả xuống ở trên, một hàm gọi là "showuser []" được thực thi.

  1. Hàm được kích hoạt bởi sự kiện Onchange.
  2. Đây là mã HTML:
  3. Hàm showuser [str] {& nbsp; if [str == ""] {& nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ["txthint"]. & nbsp; } other {& nbsp; & nbsp; & nbsp; & nbsp; var xmlHttp = new xmlHttpRequest []; & nbsp; & nbsp; & nbsp; xmlHttp.onReadyStateChange = function [] {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; if [this.ReadyState == 4 && this.status == 200] {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ["txthint"]. Internhtml = this.responsetext; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;} thật]; & nbsp; & nbsp; & nbsp; xmlhttp.send []; & nbsp; }}


Bài Viết Liên Quan

Chủ Đề