php입력데이터 유효성 확인하기Check input data validity

보라색 부분은 form_action5-1.php파일에서 유효성 확인을 위해 form5-1.php파일에서 자바스크립트 폼 체크 부분을 삭제합니다.
form_action5-1.php파일에서 값을 체크하는데 자바스크립트로 폼체크 하는 부분과 유사합니다.
다만 하늘색 부분은 php에서 자바스크립트 실행 하는 부분입니다.
alert()함수로 메세지를 보내고 페이지 이동을 합니다.
window.location.href='[page]’; 대신에 history.go(-1);을 쓸 수도 있는데 잘못된 곳으로 이동할 가능성이 큽니다.
그래서 window.location.href='[page]’;를 사용하는게 더 효율적입니다.

The purple part deletes the JavaScript form check part from the form5-1.php file to confirm validity in the form_action5-1.php file.
Checking the value in the form_action5-1.php file is similar to checking the form using JavaScript.
However, the light blue part is the part where JavaScript is executed in PHP.
Send a message using the alert() function and move to the page.
window.location.href='[page]’; You could use history.go(-1); instead, but it would most likely take you to the wrong place.
So it is more efficient to use window.location.href='[page]’;

아래의 링크를 통해서 테스트 할 수 있습니다
You can test it through the link below
https://freelifemakers.org/lec/form5-1.php

form5-1.php
<?php

$fileName = "./form_action5-1.php";

if(!is_file($fileName)){
    $form_action_name = basename($_SERVER['PHP_SELF']);
}else{
    $form_action_name = "./form_action5-1.php";
}
?>

<!DOCTYPE html>
<html>
    <head>
      <title>Bootstrap 5 webform</title>
      <!--이 부분은 html코멘트 영역 This part is the html comment area,마임타입설정 Mime type settings//-->
      <meta charset="utf-8">
      <!-- 모바일폰 스크린 옵션 Mobile phone screen options//-->
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!--부트스트랩5 CDN방식 링크 Bootstrap 5 CDN method link//-->
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
      <script>
        function frmSubmit(){
            frm.submit();
        }
      </script>
    </head>
    <body>  
    <div class="container-sm">    
        <form id="frm" action="<?php echo $form_action_name; ?>" method="post">
        <div class="mb-3">
            <label for="name" class="form-label">이름(Name):</label>
            <input type="text" class="form-control" id="name" placeholder="당신의 이름을 입력하세요(Pleas enter your name)" name="name" required>
            <div id="errorName"></div>
        </div>

        <div class="mb-3">
        <label for="email" class="form-label">Email:</label>
            <input type="text" class="form-control" id="email" placeholder="ID@exmale.com" name="email" required>
            <div id="errorEmail"></div>
        </div>

        <div class="mb-3">
        <label for="website" class="form-label">Website:</label>
            <input type="text" class="form-control" id="website" placeholder="example.com" name="website">
            <div id="errorWebsite"></div> 
        </div>
        
        <div class="mb-3">
            <label for="pwd" class="form-label">Title:</label>
            <input type="text" class="form-control" id="title" placeholder="제목을 입력하세요(Please enter the post title)" name="title" required>
            <div id="errorTitle"></div>
        </div>

        <div class="mb-3">
            <label for="pwd" class="form-label">Password:</label>
            <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd" required>
            <div id="errorPwd"></div>
        </div>        

        <div class="mb-3">
            <label for="comment" class="form-label">Comment:</label>    
            <textarea id="comment"name="comment" rows="5" cols="40" class="form-control"></textarea>
            <div id="errorComment"></div>
        </div>

        <div class="mb-3">
            <div class="form-check mb-3">
            <label class="form-check-label">Remember me</label>
            <input class="form-check-input" type="checkbox" name="remember">
            </div>
        </div>
            <button type="button" class="btn btn-primary" onClick="frmSubmit();">Submit</button>
        </form>
    </div>    
    </body>
</html>   
form_action5-1.php
<?php  

if($_SERVER['REQUEST_METHOD'] == "POST"){

   $f_name = $_POST['name'];
   $f_pwd = $_POST['pswd'];
   $f_email = $_POST['email'];
   $f_title = $_POST['title'];
   $f_website = $_POST['website'];
   $f_comment = $_POST['comment'];

   // post data blank check
   if($f_name == ""){
    echo ("<script>
            alert('이름을 입력하세요(Please Enter Your Name).');
            window.location.href = './form5-1.php';
           </script>
        ");
    exit; 
   }else if($f_email == ""){ 
    echo ("<script>
           alert('Email을 입력하세요(Please Enter Your Email).');
           window.location.href = './form5-1.php';
           </script>
        ");
    exit;   
   }else if($f_title == ""){ 
    echo ("<script>
           alert('글 제목을 입력하세요(Please Enter Your Post Title).');
           window.location.href = './form5-1.php';
           </script>
        ");
    exit;                  
   }else if($f_comment == ""){
      echo ("<script>
              alert('글내용을 입력하세요(Please Enter The post content).');
              window.location.href = './form5-1.php';
             </script>
          ");
      exit;     
   }else if($f_pwd == ""){
    echo ("<script>
            alert('패스워드를 입력하세요(Please Enter Your Password).');
            window.location.href = './form5-1.php';
           </script>
        ");
    exit;    
   }   

   // email check
   if (!preg_match("/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/",$f_email)) {
      echo ("<script>
            alert('정확한 Email을 입력하세요(Please enter the correct Email).');
            window.location.href = './form5-1.php';
            </script>
         ");
       exit;
   }
   // URL check
    if (!preg_match("/^(https?:\/\/)?([a-z0-9-]+\.)+[a-z]{2,7}(\/\S*)?$/i",$f_website)) {
   echo ("<script>
         alert('정확한 URL을 입력하세요(Please enter the correct URL).');
         window.location.href = './form5-1.php';
         </script>
      ");
    exit;
   }


}

   echo "REQUEST_METHOD:".$_SERVER['REQUEST_METHOD']."<br>";
   echo "이름(NAME):".$f_name."<br>";
   echo "이메일(EMAIL):".$f_email."<br>";
   echo "제목(TITLE):".$f_title."<br>";
   echo "웹사이트(WEBSITE):".$f_website."<br>";
   echo "글 내용(CONTENT):".$f_comment."<br>";
   echo "패스워드(PASSWORD):".$f_pwd;

?>

Leave a Reply

Your email address will not be published. Required fields are marked *