상세 컨텐츠

본문 제목

[PHP] 아이디 찾기 ajax

php

by 2hansoul 2021. 11. 29. 18:04

본문

반응형

search.php

<html>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<script> //클라이언트 페이지에 보여주기 위해 ajax사용 그냥 서버페이지에서 보여주고 싶으면 사용안하면 됌
 function add() {
	 var data = $('#sear').val();

        $.ajax({
            url: "/searchRE.php",     //입력값을 보낼 주소
            type: "get",              //get,post 데이터 타입
            data: {sear: $('#sear').val()},     //
			success : function(data){
			     $('#aaa').html(data);    //p테그 id 값에 받아온 데이터 출력
				  console.log(data);
			},

    });
 }

</script>

<p id="aaa"></p>
<input type="text" id="sear" placeholder="id입력">
<button onclick="add()" >찾기</button>
</html>

searchRE.php

<?php
include  $_SERVER['DOCUMENT_ROOT']. "/test/dbconn.php";

$sear = $_GET['sear'];




$sql="select*from login where id='$sear'";
$row=mysqli_query($conn,$sql);

$total= mysqli_num_rows($row);  //db에서 값이 몇개 존재 하는지 알수 있다 0개면 아이디가 없기 때문에 찾을수 없음
echo $total."<br>";

if($total==1){

while($result = mysqli_fetch_array($row)){  //배열을 while 데이터를 뽑아 준다
echo "id : ".$result['id']."<br>"."name : ".$result['name']."<br>"."email : ".$result['email']."<br>"."date : ".$result['date'];
}

}else{
   	echo "<script>alert('없는 아이디입니다');history.back();</script>";
	 return false;
}




?>

 

간단한 ajax로 아이디 찾기 예제 

반응형

관련글 더보기

댓글 영역