0

I'm doing a register form and I'm trying to write a code that will show error if user does not fill all fields. It works okay but for some reason it works only if I enter email. If I leave all fields empty and click register, I don't get any error and with all fields empty my cursor goes to email field and gets activated (this field) when I click register button. Something is wrong with this email but I cannot find any problem, so what is wrong?

PHP source code:

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>
James Moron
  • 9
  • 1
  • 3

5 Answers5

0

Because the other fields are type="text" and empty are considered as empty strings "". Try to check also if they are not empty. ;)

For example:

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

For email and password you can skip empty I guess if you make them required in HTML5.

oetoni
  • 3,269
  • 5
  • 20
  • 35
0

I don't think they are empty. They will either be undefined or "". Use print_r($_POST['name']) for each one and see what they come back as. Then you will see if they are empty or not.

newCoder
  • 13
  • 5
0

The onblur and onfocus events make the value of each input always populated so when you submit each field has its default value, or what you changed it to. A better solution to this would be using the placeholder attribute. This will keep the values empty until a user populates them.

<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>

This also should function better for the password field because I suspect you previously had the password as 8 bullet points.

Server side you could verify this was the cause by outputting the $_POST array with var_dump or print_r, or by iterating over it foreach($_POST as $name => value).

chris85
  • 23,846
  • 7
  • 34
  • 51
0

First of all, your PHP Code is Correct. but your HTML Form have some Technical Error. you must set placeholder attribute for show Field name in input boxs, becasue when onBlur event was run, the value was set for field and string sent to server, and that NOT Empty ! in this way your PHP code does't work for check empty values. the secondary work you must do, check field in client side for not empty by easily set required attribute for each input element. I wrote correct html tags that you must use.

<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>

for PHP code you can use Foreach as simply way :

$error="";
foreach($_POST as $key => $value) {
    if(empty($_POST[$key]))
    {
        $error="Fill all fields!";
        break;
    }
}
Hamed Taheri
  • 164
  • 2
  • 11
-1

you must you placeholder instead of value.

<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>
Dhairya Lakhera
  • 4,445
  • 3
  • 35
  • 62