Jump to content

[SOLVED] Faulty Form Validation... STUMPED


christofurr

Recommended Posts

My form validation is returning every single failure message when all inputs meet condtions. I split the validation script into several files and included them in the form file. They're sort of long scripts. I hope this isn't breaking the rules.

 

Here's the form. I used PHP here to keep the user's trimmed input in the fields and to include the validation files. Don't mind the HTML:

 

<html>
<head>
<style type="text/css">
.dark {background-color: lightsteelblue; margin-right: 0px; margin-left: 0px; padding-right: 2px; padding-left: 3px;}
.darker {background-color: #a9a9a9; margin-right: 0px; margin-left: 0px; padding-right: 2px; padding-left: 3px;}
.light {background-color: lightgrey; margin-right: 0px; margin-left: 0px; padding-right: 2px; padding-left: 3px;}
.warn {font-size: 12px; color: red;}
</style>
</head>
<body>

<p>

<table border="0">

<tr>
<td class="darker" colspan="3" align="center"><h4>Register</h4></td>
</tr>

<tr>
<td class="light" align="right">First Name: </td>
<td class="light"><form action="form10.php5" method="post">
<input type="text" name="name1" value="<?php $nam1trim = trim($_POST["name1"]); echo $nam1trim; ?>" size="35" maxlength="20" /><br />
<?php include ("validname1.php5"); ?>
</td>
<td class="light" style="font-size: 11px; width: 220px;">Max of 20 letters. If you have a freakishly long name, please abbreviate or... just give us a fake one, but make it short. </td>
</tr>
<tr>
<td class="dark" align="right">Last Name: </td>
<td class="dark"><input type="text" name="name2" value="<?php $nam2trim = trim($_POST["name2"]); echo $nam2trim; ?>" size="35" maxlength="20" /><br />
<?php include ("validname2.php5"); ?>
</td>
<td class="dark" style="font-size: 11px; width: 220px;">Same as above. =P</td>
</tr>
<tr>
<td class="light" align="right">Desired Nickname: </td>
<td class="light"><input type="text" name="nickname" value="<?php $nicktrim = trim($_POST["nickname"]); echo $nicktrim; ?>" size="35" maxlength="32" /><br />
<?php include ("validnickname.php5"); ?>
</td>
<td class="light" style="font-size: 11px; width: 220px;">This is who you'll be known as around here so get creative. You're limited to 32 letters, numbers, and spaces. Nothing else.</td>
</tr>
<tr>
<td class="dark" align="right">Password: </td>
<td class="dark"><input type="password" name="pw" size="35" maxlength="20" /><br />
<?php include ("validpw.php5"); ?>
</td>
<td class="dark" style="font-size: 11px; width: 220px;"><b>Do not use the same password that you use for your RuneScape account(s)</b> or any other account for that matter. 20 letters and numbers max, case insensitive.</td>
</tr>
<td class="light" align="right">Verify Password: </td>
<td class="light"><input type="password" name="vpw" size="35" maxlength="20" /><br />
<?php include ("validvpw.php5"); ?>
</td>
<td class="light" style="font-size: 11px; width: 220px;">Retype the password just to be sure you're not making any typos... b'cause it happens.</td>
</tr>
<tr>
<td class="dark" align="right">Email Address: </td>
<td class="dark"><input type="text" name="email" value="<?php echo $_POST["email"]; ?>" size="35" maxlength="50" /><br />
<?php include ("validemail.php5"); ?>
</td>
<td class="dark" style="font-size: 11px; width: 220px;">So we can email you if we have to and to verify this registration. Max of 50 characters.</td><!-- Line 120 -->
</tr>
<tr>
<td class="light" align="right">Recovery Question: </td>
<td class="light"><input type="text" name="recq" value="<?php $recqtrim = trim($_POST["recq"]); echo $recqtrim; ?>" size="35" maxlength="75" /><br />
<?php include ("validrecq.php5"); ?>
</td>
<td class="light" style="font-size: 11px; width: 220px;">Tell us what to ask you to make sure it's you who's trying to recover your password (if you ever need to). Language and grammer doesn't matter as long as you can recognize your own question. Max of 75 characters.</td>
</tr>
<tr>
<td class="dark" align="right">Recovery Answer: </td>
<td class="dark"><input type="text" name="reca" value="<?php echo $_POST["reca"]; ?>" size="35" maxlength="30" /><br />
<?php include ("validreca.php5"); ?>
</td>
<td class="dark" style="font-size: 11px; width: 220px;"><b>Beware of prying eyes as you fill this one out.</b> Tell us the answer to your secret question and remember what it is <b>EXACTLY!</b></td>
</tr>

<tr>
<td class="darker" colspan="3" align="center">
<input type="submit" value="Create Account" />
</form>
</td>
</tr>

</table>

</p>

</body>
</html>

 

Here's one of the validation scripts - validname1.php5. All of the validation scripts are basically the same, except for the last bit where I coded the failure messages especially for where the validation include would be placed:

 

<?php

$nicktrim = trim($_POST["nickname"]);
$nam1trim = trim($_POST["name1"]);
$nam2trim = trim($_POST["name2"]);
$pastrim = trim($_POST["pw"]);
$vpastrim = trim($_POST["vpw"]);
$recqtrim = trim($_POST["recq"]);


if (!empty($_POST))
{
    if (empty($_POST["name1"]))
      {$missA = "1A";}
    else
      {if (ereg('[^a-zA-Z ]', $nam1trim))
         {$missA = "1B";}
       elseif (strlen($nam1trim)>20)
         {$missA = "1C";}
      }
    if (empty($_POST["name2"]))
      {$missB = "2A";}
    else
      {if (ereg('[^a-zA-Z ]', $nam2trim))
         {$missB = "2B";}
       elseif (strlen($nam2trim)<3)
         {$missB = "2C";}
      }
    if (empty($_POST["nickname"]))
      {$missC = "3A";}
    else
      {if (ereg('[^a-zA-Z0-9 ]', $nicktrim))
         {$missC = "3B";}
       elseif (strlen($nicktrim)<3)
         {$missC = "3C";}
       elseif (strlen($nicktrim)>32)
         {$missC = "3D";}
      }
    if (empty($_POST["pw"]))
      {$missD = "4A";}
    else
      {if (ereg('[^a-zA-Z0-9]', $pastrim))
         {$missD = "4B";}
       elseif (strlen($pastrim)<6)
         {$missD = "4C";}
       elseif (strlen($pastrim)>20)
         {$missD = "4D";}
       elseif ($pastrim!=$vpastrim)
         {$missD = "4E";}
      }
    if (empty($_POST["email"]))
      {$missE = "5A";}
    else
      {if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
         {$missE = "5B";}
      }
    if (empty($_POST["recq"]))
      {$missF = "6A";}
    if (empty($_POST["reca"]))
      {$missG = "7A";}


/* This is the portion that I change when I want to display failures for different inputs. */
   if ($missA = "1A")
     {echo "<span class='warn'>You forgot to tell us your first name.</span><br />";
     }
   if ($missA = "1B")
     {echo "<span class='warn'>Your name can only consist of letters and spaces. Please check your input.</span><br />";
     }
   if ($missA = "1C")
     {echo "<span class='warn'>Your first name cannot consist of more than 20 characters.</span><br />";
     }


}

?>

 

And finally, here's the product: FORM10.PHP5.

 

The right column specifys the conditions for each input field. Even when typing correct input for each field, all failures display.

 

What am I doing wrong?

 

 

Link to comment
Share on other sites

I'm not a pro but why do you have all the includes?

 

EDIT: I read some more code.

 

I see what you are trying todo but as far as I know this isn't how forms work. You need to setup a form that sends all of the data to 1 php script that can then include the validation files. That is, if you still want to split them anyway.

 

I think you should spend a little more time reading about your topic to get a better idea of how this works. It looks like what you are trying to do right now is client side scripting and it just doesn't work that way with php. I looked through the beginners area here on phpfreaks but didn't see anything on form validation. Here's a good place to get started http://www.tizag.com/phpT/forms.php

Link to comment
Share on other sites

That's how I started off, Dog. That was back at FORM1.PHP5.

 

Problem with that is that I can't display different validation failure messages in different parts of the form. I'm going to have to have the whole form printed through PHP. I'll return if I have any problems.

Link to comment
Share on other sites

Your form validation is showing all errors because your comparisons are incorrect.

 

Look at the following code:

if ($missA = "1A")

 

Here you are assigning the variable missA the value "1A" and then testing whether its true. All assignments will return true and so it echo's out all error messages

 

Instead you should use the comparison operator (==)

Link to comment
Share on other sites

So does that mean that my validations will work properly if I change the operators?

 

Probably not, your problem isn't your syntax it's your logic.

 

You need to create a single <form> that can accept all your clients entries. That form will POST or GET data to your php script depending on how you want it to send data. Then with your php script you validate those entries.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.