Thursday, 3 March 2022

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>

0 comments:

Post a Comment