2017-11-11 4 views
0

저는 등록 양식을 작성 중이며 사용자가 모든 필드를 채우지 않으면 오류를 표시하는 코드를 작성하려고합니다. 괜찮습니다.하지만 어떤 이유로 이메일을 입력 할 때만 작동합니다. 모든 입력란을 비워두고 등록을 클릭하면 오류가 발생하지 않고 모든 입력란이 비어있는 상태에서 등록란을 클릭하면 커서가 이메일 입력란으로 이동하여 활성화됩니다 (이 입력란). 이 이메일에 문제가 있어도 문제는 찾을 수 없으므로 잘못되었습니다.PHP가 필드가 비어 있는지 확인하십시오.

PHP 소스 코드 :

if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['email']) || empty($_POST['password'])) 
{ 
    $error="Fill all fields!"; 
} 

HTML :

<form action="register.php" method="POST"> 
    <input style="margin-top:10px" type="text" value="First Name" name="fname" onblur="if (this.value == '') {this.value = 'First Name';}" onfocus="if (this.value == 'First Name') {this.value = '';}" /> 

    <input type="text" value="Last Name" name="lname" onblur="if (this.value == '') {this.value = 'Last Name';}" onfocus="if (this.value == 'Last Name') {this.value = '';}" /> 

    <input type="email" value="Adres e-mail" name="email" onblur="if (this.value == '') {this.value = 'Adres e-mail';}" onfocus="if (this.value == 'Adres e-mail') {this.value = '';}" /> 

    <p><input type="password" value="Password" name="password" onblur="if (this.value == '') {this.value = 'Password';}" onfocus="if (this.value == 'Password') {this.value = '';}" /></p> 

    <button type="submit" name="submit"> Register</button> </br> 
    </br> 

</form> 
+0

이 질문의 상태가 무엇입니까? – chris85

답변

0

다른 필드가 type="text"을하고 빈은 빈 문자열 ""으로 간주되기 때문에. 비어 있지 않은지 확인해보십시오. ;) 예를 들어

: 이메일과 비밀번호를 들어

if(isset($_POST["fname"]) && (strlen($_POST["fname"]) > 0) && isset($_POST["lname"]) && (strlen($_POST["lname"]) > 0) && isset($_POST["email"]) && isset($_POST["password"])){ 
    //your code 
} 

당신은 당신이 그들을 HTML5에 필요한 만들면 같아요 빈 건너 뛸 수 있습니다.

+1

클라이언트 측 유효성 검사에 의존하지 마십시오. 'isset'과'! empty'는 기본적으로 같은 것입니다. 'empty()는 기본적으로! isset ($ var) || $ var == false'-http : //php.net/manual/en/function.empty.php – chris85

+0

안녕하세요 chris85는 이제 더 나아졌습니다. 그냥 ... 길이를 확인하십시오. – oetoni

+0

@ chris85 isset! 비어있는 똑같은 것이 아니다 –

0

나는 그것이 비어 있다고 생각하지 않습니다. 정의되지 않거나 ""입니다. 각각 하나씩 print_r($_POST['name'])을 사용하고 그들이 무엇으로 돌아 왔는지 확인하십시오. 그렇다면 그들이 비 었는지 알 수 있습니다.

+0

마찬가지로 ... print_r ($ _ POST [ 'fname']); – newCoder

0

자리 표시 자 대신 이 필요합니다.

<form action="index3.php" method="POST" > 

    <input style="margin-top:10px" type="text" placeholder="First Name" name="fname" 
    onblur="if (this.placeholder == '') {this.placeholder = 'First Name';}" 
    onfocus="if (this.placeholder == 'First Name') {this.placeholder = '';}" /> 

    <input type="text" placeholder="Last Name" name="lname" 
    onblur="if (this.placeholder == '') {this.placeholder = 'Last Name';}" 
    onfocus="if (this.placeholder == 'Last Name') {this.placeholder = '';}" /> 

    <input type="email" placeholder="Adres e-mail" name="email" 
    onblur="if (this.placeholder == '') {this.placeholder = 'Adres e-mail';}" 
    onfocus="if (this.placeholder == 'Adres e-mail') {this.placeholder = '';}" /> 

    <p><input type="password" placeholder="Password" name="password" 
    onblur="if (this.placeholder == '') {this.placeholder = 'Password';}" 
    onfocus="if (this.placeholder == 'Password') {this.placeholder = '';}" /></p> 

    <button type="submit" name="submit"> Register</button> </br></br> 

</form> 
+0

당신의 대답은 여전히'value's를 사용하고 있습니다. – chris85

0

onbluronfocus 이벤트는 각 필드는 디폴트 값, 또는 당신이 그것을에 변화를 가지고 제출할 때 각 입력의 value은 항상 채워합니다. 더 나은 해결책은 placeholder 속성을 사용하는 것입니다. 이렇게하면 사용자가 채울 때까지 value이 비어있게됩니다.

<form action="register.php" method="POST"> 
    <input style="margin-top:10px" type="text" value="First Name" name="fname" placeholder="First Name" /> 
    <input type="text" value="Last Name" name="lname" placeholder="Last Name" /> 
    <input type="email" value="Adres e-mail" name="email" placeholder="Adres e-mail" /> 
    <p><input type="password" value="Password" name="password" placeholder="Password" /></p> 
    <button type="submit" name="submit"> Register</button> </br> 
    </br> 
</form> 

내가 이전에 password 8로 글 머리 기호를 가지고 생각하기 때문에이 또한 암호 필드에 대한 더 나은 작동합니다.

서버 쪽또는 print_r을 사용하여 $_POST 배열을 출력하거나 foreach($_POST as $name => value)을 반복하여 서버 측에서 확인할 수 있습니다.

0

먼저 PHP 코드가 올바른 것입니다. HTML 양식에는 기술적 인 오류가 있습니다. onBlur 이벤트가 실행될 때 입력란에 show field name에 대한 placeholder 속성을 설정해야합니다.이 값은 서버에 전송 된 필드 및 문자열에 설정되었으므로 비어 있지 않습니다. 이런 식으로 PHP 코드는 빈 값을 검사 할 수 없습니다. 당신이해야 할 부차적 인 작업, 각 입력 요소에 대해 required 속성을 쉽게 설정하여 클라이언트 측의 필드를 비어 있지 않은지 확인하십시오. 내가 사용해야하는 올바른 HTML 태그를 작성했습니다.

당신은 단순히 방법으로의 Foreach을 사용할 수있는 PHP 코드에 대한

<form action="register.php" method="POST"> 
 
     <input style="margin-top:10px" type="text" name="fname" placeholder="First Name" required /> 
 

 
     <input type="text" name="lname" placeholder="Last Name" required /> 
 

 
     <input type="email" name="email" placeholder="Adres e-mail" required /> 
 

 
     <p><input type="password" name="password" placeholder="Password" required /></p> 
 

 
     <button type="submit" name="submit"> Register</button> </br> 
 
     </br> 
 

 
    </form>

:

$error=""; 
foreach($_POST as $key => $value) { 
    if(empty($_POST[$key])) 
    { 
     $error="Fill all fields!"; 
     break; 
    } 
} 
관련 문제