Skip to main content

PHP#31MySQL#27 - Ajax2

//checkuser.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-3.6.0.js"></script>
</head>

<body>
<script type="text/javascript"> 
$(document).ready(function(){
$("#username").blur(function(){
$.ajax({
                    type:"POST",
                    url:"xuly_checkuser.php",
                    data:"user="+$("#username").val(),
                    success:function(abc)
{
                        if(abc=="1")
$("#kq").html("User này đã tồn tại. Mời bạn chọn user khác!");
else
{
$("#kq").html("User này chưa có. Bạn có thể sử dụng");
$("#dangky").removeAttr("disabled");
}
                    }
})
})
//Viết code cho sự kiện focus lên username:
$("#username").focus(function(){
$("#dangky").attr("disabled",true);
$("#kq").html("");
})
    });
</script>

<form method="get" id="form1" name="form1" action="">
  <p>
    <label for="textfield">Họ Tên:</label>
    <input type="text" name="hoten" id="hoten">
  </p>
  <p>
    <label for="username">Username:</label>
    <input type="text" name="username" id="username"><br/>
    <div id="kq"></div>
  </p>
  <p>
    <label for="password">Password:</label>
    <input type="password" name="password" id="password">
  </p>
  <p>
    <label for="confirmpassword">Re Password:</label>
    <input type="password" name="confirmpassword" id="confirmpassword">
  </p>
  <p>
    <input type="submit" name="dangky" id="dangky" value="Đăng ký" disabled>
  </p>
</form>
</body>
</html>

//xuly_checkuser.php

<?php 
if(isset($_POST['user']))
{
include("connect.php");
$user=$_POST['user'];
$sl="select * from webtm_user where Username='$user'";
$kq=mysqli_query($link,$sl);
if(mysqli_num_rows($kq))
echo "1";
else
echo "0";
}
?>

//locsp.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-3.6.0.js"></script>

</head>

<body>
<script type="text/javascript"> 
$(document).ready(function(){
lay_sp()
$("#loaisp").change(function(){
lay_sp()
})
    });
function lay_sp()
{
$.ajax({
type:"GET",
url:"xuly_locsp.php",
data:"idloai="+ $("#loaisp").val(),
success: function(abc)
{
$("#sanpham").html(abc);
}
})
}
</script>

<form id="form1" name="form1" method="get">
 <?php
  include("connect.php");
$slloai="select * from webtm_loaisp";
$kqloai=mysqli_query($link,$slloai);
?>
  <p>
    <label for="loaisp">Loại sp::</label>
    <select name="loaisp" id="loaisp">
     <?php
$idLoai=0;
//if(isset($_GET['loaisp'])) $idLoai=$_GET['loaisp'];
while($dloai=mysqli_fetch_array($kqloai))
{
if($idLoai==0)$idLoai=$dloai['idLoai'];
?>
      <option value="<?php echo $dloai['idLoai'];?>" <?php if(isset($_GET['loaisp'])&&$_GET['loaisp']==$dloai['idLoai']){ echo "selected";$idLoai=$_GET['loaisp'];}?>><?php echo $dloai['TenLoai'];?></option>
      <?php }?>
    </select>
  </p>
 
</form>
<div id="sanpham"></div>
</body>
</html>

//xuly_locsp.php

<?php
if(isset($_GET['idloai']))
{
$idLoai=$_GET['idloai'];
include("connect.php");
$slsp="select TenSP, UrlHinh, Gia from webtm_sanpham where idLoai=$idLoai";
$kqsp=mysqli_query($link,$slsp);
?>
<table width="500" border="1" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <th scope="col">STT</th>
      <th scope="col">Tên Sản Phẩm</th>
      <th scope="col">Hình</th>
      <th scope="col">Giá</th>
    </tr>
    <?php
$stt=1;
while($dsp=mysqli_fetch_array($kqsp)){
?>
    <tr>
      <td><?php echo $stt++;?></td>
      <td><?php echo $dsp['TenSP'];?></td>
      <td><img src="<?php echo $dsp['UrlHinh'];?>" width="100"  alt=""/></td>
      <td><?php echo $dsp['Gia'];?></td>
    </tr>
    <?php }?>
  </tbody>
</table>
<?php }?>


LỌC CHỦNG LOẠI --> LOẠI --> SẢN PHẨM theo AJAX:

//locsp.php:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-3.6.0.js"></script>

</head>

<body>
<script type="text/javascript"> 
$(document).ready(function(){
lay_loai();
$("#chungloai").change(function(){
lay_loai();
})
})

function lay_loai()
{
$.ajax({
type:"GET",
url:"xuly_locsp0.php",
data:"idcl="+ $("#chungloai").val(),
success: function(tam)
{
$("#loai_sp").html(tam);
}
})
}    
</script>

<form id="form1" name="form1" method="get">
 <p>
    <label for="chungloai">Chủng loai sp:</label>
    <select name="chungloai" id="chungloai">
<?php
include("connect.php");
$sl="select * from webtm_chungloaisp";
$kq=mysqli_query($link,$sl);
while($d=mysqli_fetch_array($kq))
{
?>
      <option value="<?php echo $d['idCL'];?>"><?php echo $d['TenCL'];?></option>
      <?php }?>
    </select>
  </p>
 <p id="loai_sp"></p>
</form>
<div id="sanpham"></div>
</body>
</html>

//xuly_locsp0.php

<script type="text/javascript" src="jquery-3.6.0.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
lay_sp()
$("#loaisp").change(function(){
lay_sp()
})
    });
function lay_sp()
{
$.ajax({
type:"GET",
url:"xuly_locsp.php",
data:"idloai="+ $("#loaisp").val(),
success: function(abc)
{
$("#sanpham").html(abc);
}
})
}
</script>

 <?php
  include("connect.php");
$slloai="select * from webtm_loaisp where idcl=".$_GET['idcl'];
$kqloai=mysqli_query($link,$slloai);
?>

    <label for="loaisp">Loại sp::</label>
    <select name="loaisp" id="loaisp">
     <?php
$idLoai=0;
//if(isset($_GET['loaisp'])) $idLoai=$_GET['loaisp'];
while($dloai=mysqli_fetch_array($kqloai))
{
if($idLoai==0)$idLoai=$dloai['idLoai'];
?>
      <option value="<?php echo $dloai['idLoai'];?>" <?php if(isset($_GET['loaisp'])&&$_GET['loaisp']==$dloai['idLoai']){ echo "selected";$idLoai=$_GET['loaisp'];}?>><?php echo $dloai['TenLoai'];?></option>
      <?php }?>
    </select>

Comments

Popular posts from this blog

PHP#33MySQL#29 - Đếm lượt truy cập (KẾT THÚC KHÓA HỌC)

//hitcounter.php <?php session_start();  ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php  include("../connect.php"); //cập nhật số luowhtj truy cập: $sl="update counter set cnt=cnt+1"; mysqli_query($link,$sl); //Lấy dữ liệu số lượt truy cập để hiển thị ra web: $sl2="select * from counter"; $kq=mysqli_query($link,$sl2); $d=mysqli_fetch_array($kq); //Định dạng: $chuoi=str_pad($d['cnt'],6,"0",STR_PAD_LEFT); $luottrycap=""; for($i=0;$i<strlen($chuoi);$i++) { $tam=substr($chuoi,$i,1); $luottrycap.="<img src='images/$tam.png'/>"; } /*-Khi user truy cập vào website: +lần đầu tuy cập: Lưu thông tin của user lên csdl +Sau lần đầu tiên: cập nhtaaj lastvisit - Xóa các dòng dữ liệu quá 1 phút (Quy ước thời gian để khẳng định online hay offline: 1 phút) - Thống kê số liệu người dùng o...

Bài 2 - SOME OTHER TIME - Vào dịp khác

PHP#6 - MySQL#2 - Thêm, Sửa, Xóa trong SQL, tìm kiếm

  1. Tạo bảng khachhang với các thuộc tính sau: 2. Dùng lệnh SQL : - Thêm dữ liệu cho bảng khachhang: => INSERT INTO khachhang VALUES(1, "nguyen VAN thanh", "aa", "123", "Q1", "a") INSERT INTO khachhang VALUES(2, "NGUYEN thi Linh", "bb", "123456", "q1", "123456", "b") INSERT INTO khachhang VALUES(3, "Nguyen Van Teo", "teovn", "123456", "aa", "88", "lo@a.com") Sửa Địa chỉ của user aa thành Q10 =>UPDATE khachhang SET DiaChi="Q10" WHERE UserName="aa" Sửa số điện thoại của user teonv thanh 093445212 =>UPDATE khachhang SET DienThoai="093445212" WHERE UserName="teovn" Xóa tất cả user nào có email la b =>DELETE FROM khachhang WHERE Email="b" //KẾT QUẢ //Bài tiếp theo: Câu lệnh Select đơn giản Chỉ có phần select from. Có thể có thêm where. Ví dụ: 1.   SELECT ...