[php] $ _POST로 설정된 경우

한 페이지에 다른 페이지로 제출하는 양식이 있습니다. 거기에서 입력 메일이 채워져 있는지 확인합니다. 그렇다면 무언가를하고 채워지지 않으면 다른 일을하십시오. 빈 양식을 보내도 항상 설정되어 있다고 말하는 이유를 이해하지 못합니다. 무엇이 없거나 잘못 되었습니까?

step2.php :

<form name="new user" method="post" action="step2_check.php">
    <input type="text" name="mail"/> <br />
    <input type="password" name="password"/><br />
    <input type="submit"  value="continue"/>
</form>

step2_check :

if (isset($_POST["mail"])) {
    echo "Yes, mail is set";
} else {
    echo "N0, mail is not set";
}



답변

대부분의 양식 입력은 채워지지 않더라도 항상 설정되므로 비어 있는지도 확인해야합니다.

!empty()이미 두 가지를 모두 확인 하므로 다음을 사용할 수 있습니다.

if (!empty($_POST["mail"])) {
    echo "Yes, mail is set";
} else {
    echo "No, mail is not set";
}


답변

사용 !empty대신에 isset. 배열이 수퍼 글로벌이고 항상 존재 하기 $_POST때문에 isset은 true를 반환합니다 $_POST.

또는 더 나은 사용 $_SERVER['REQUEST_METHOD'] == 'POST'


답변

php.net 에서 isset

var가 존재하고 NULL이 아닌 값이 있으면 TRUE를 반환하고 그렇지 않으면 FALSE를 반환합니다.

빈 공간은 세트로 간주됩니다. 모든 null 옵션을 확인하려면 empty ()를 사용해야합니다.


답변

양식을 비워두면 $ _POST [ ‘mail’]은 계속 전송되지만 값은 비어 있습니다. 필드가 비어 있는지 확인하려면 확인해야합니다.

if(isset($_POST["mail"]) && trim($_POST["mail"]) != "") { .. }


답변

입력 텍스트 양식에 다음 속성을 추가하십시오 required="required".. 양식을 작성하지 않으면 사용자가 양식을 제출할 수 없습니다.

새 코드는 다음과 같습니다.

<form name="new user" method="post" action="step2_check.php">
<input type="text" name="mail" required="required"/> <br />
<input type="password" name="password" required="required"/><br />
<input type="submit"  value="continue"/>
if (isset($_POST["mail"])) {
    echo "Yes, mail is set";
}


답변

다음을 간단히 사용할 수 있습니다.

if($_POST['username'] and $_POST['password']){
  $username = $_POST['username'];
  $password = $_POST['password'];
}

또는 empty () 사용

if(!empty($_POST['username']) and !empty($_POST['password'])){
  $username = $_POST['username'];
  $password = $_POST['password'];
}


답변

사용 !empty()대신에 isset(). isset()귀하의 경우 항상 진실을 반환 하기 때문 입니다.

if (!empty($_POST["mail"])) {
    echo "Yes, mail is entered";
} else {
    echo "No, mail is not entered";
}