Perl에서 Jquery를 이용해 email 중복 검사 하기

Perl에서 Jquery를 이용해 email 중복을 검사하는 방법입니다.

Perl에서 Jquery를 이용해 email 중복 검사 하기

-'Perl에서  Jquery를 이용한 email 중복체크 하기'

저는 이번에 Perl을 처음 건드려 봅니다.
'펄 세계엔, 한줄의 코드로 컴퓨터를 주므르는 고수들이 살고 있다!'
한참 전에 이런 소리를 들어 봤던거 같군요.
그때 든 생각은.
'한줄? 아! 디버깅하기 힘들겠구나.' 였어요.
인간 친화적이라기보다 기계 친화적인 언어라고 생각했었죠.
펄로 만들어진 PHP는 친숙하지만 Perl이라면 왠지 거리감이 느껴지는 언어였어요.
그런데 Perl에서도 요즘 흔히 쓰는 Jquery를 이용한 Ajax 구현이 가능합니다.
덕분에 조금 가까워 졌네요.:D


duplicate_test.html
<html><head/><body>
<INPUT maxLength=12 name="email" id="email" size="20"><br/>

<div id="duplicateResult"></div>
<script type="text/javascript" src="/rfo/jquery.js"></script>

<script type="text/javascript" src="/rfo/duplicate.js"></script>
</body></html>


duplicate.js
$(document).ready(function(){
  $("#email").keyup(function() {
    var email = $('#email').attr('value'); // get email
        if (email) { // values are not empty   
      $.ajax({
        type: "GET",
        url: "/cgi-bin/duplicate.pl", // URL of the Perl script
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "email=" + email,       
        // script call was not successful
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          $('div#duplicateResult').text("responseText: " + XMLHttpRequest.responseText
            + ", textStatus: " + textStatus
            + ", errorThrown: " + errorThrown);
          $('div#duplicateResult').addClass("error");
        }, // error
        // script call was successful
        // data contains the JSON values returned by the Perl script
        success: function(data){
          if (data.vacancy) { // script returned error
            $('div#duplicateResult').html("<span style='color:blue;font-size:14px;font-weight:bold;'>" + data.vacancy+"</span>");
            $('div#duplicateResult').addClass("vacancy");
          } // if
          else { // login was successful
            $('div#duplicateResult').html("<span style='color:red;font-size:14px;font-weight:bold;'>" + data.occupied+"</span>");
            $('div#duplicateResult').addClass("occupied");
          } //else
        } // success
      }); // ajax
    } // if
    else {
      $('div#duplicateResult').text("아이디를 입력해 주세요.");
      $('div#duplicateResult').addClass("error");
    } // else
    $('div#duplicateResult').fadeIn();
    return false;
  });
});


duplicate.pl

!/usr/bin/perl

use CGI;
use DBI;
use strict;
use warnings;
use rfoconfig;


read the CGI params

my $cgi = CGI->new;
my $email = $cgi->param("email");


connect to the database

my $dbh = DBI->connect("DBI:mysql:testDB:localhost","user","password");


check the username and password in the database

my $statement = qq{SELECT COUNT(*) FROM user WHERE email=?};
my $sth = $dbh->prepare($statement)  or die $dbh->errstr;
$sth->execute($email)  or die $sth->errstr;
my ($duplicatekey) = $sth->fetchrowarray;

my $json = ($duplicate_key) ?
  qq{{"occupied" : " $email는 이미 있는 아이디 입니다."}} :
  qq{{"vacancy" : "$email을 사용하실 수 있습니다."}};


return JSON string


"DBI:mysql:testDB:localhost","user","password"
우선 위 코드에서 testDB:localhost , user, password는 테스트 환경에 맞게 설정해 주세요.

print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;
아래 파일은 Document root/ 에 넣으세요.

  • Jquery.js (http://jquery.com 에서 다운로드 받으세요.)
  • duplicate.js
  • duplicate_test.html

아래 파일은 Document root/cgi-bin 에 넣으세요.

  • duplicate.pl


 

참고자료
Very simple login using Perl, jQuery, Ajax, JSON and MySQL

by 月風



by


Tags : , , , ,

  • 재미있게 읽으셨나요?
    광고를 클릭해주시면,
    블로그 운영에 큰 도움이 됩니다!