-1

So I have created a from which has 2 inputs. a text box where the user can type his username and a text box were he types in his password. then the PHP validation where if the text box for Username is empty then give an error message or if there is text in it, see if the text contains only numbers. if it does not give an error message again. The PHP validation for the password is the same principal. btw the forms loads to the same page as to where it is being created. But for some reason when it loads onto the broswer (loads fine) and I type in sybols on the Username text field and hit submit, the error does not come up. I really hope you guys can spot the problem cause this is driving me crazy.

<!DOCTYPE html>
<html>
   <head>
      <style>
         .error {
         color: red;
         }
      </style>
      <title>Register</title>
   </head>
   <body>
      <form action="#" method="POST">
         <h1 id="Signup">Sign Up</h1>
         <input type="text" name="Username" placeholder="Please Enter a Username">
         <span class="error"><? echo $errorusername;?></span>
         <br>
         <input type="password" name="Password" placeholder="Please Enter a Password">
         <span class="error"><? echo $errorpass;?></span>
         <br>
         <input type="Submit" id ="Submit">
      </form>
   </body>
</html>

<?php
$errorpass = "";
$errorusername = "";

if (isset($_POST['Submit']))
    {
    if (empty($_POST['Username']))
        {
        $errorusername = "name is required";
        }
      else
        {
        $Username = $_POST['Username'];
        if (!preg_match("/^[a-zA-Z ]*$/", $Username))
            {
            $errorusername = "Only numbers allowed";
            }
        }

    if (empty($_POST['password']))
        {
        $errorusername = " A password is required";
        }
      else
        {
        $password = $_POST['password'];
        if (!preg_match("/^[a-zA-Z ]*$/", $password))
            {
            $errorpass = "Need to contain numbers and symbols";
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • needs to be php – Shank Jun 29 '16 at 17:39
  • Place the php code at the top of the page. – copeg Jun 29 '16 at 17:40
  • Fred -ii- I dont think is question is related to the dupe link u posted – Shank Jun 29 '16 at 17:43
  • Quick hint, your `submit` button has no `name`, so `isset($_POST['Submit'])` should be always false... – FirstOne Jun 29 '16 at 17:46
  • @matty check this out http://www.w3schools.com/php/php_forms.asp – Shank Jun 29 '16 at 17:47
  • ohh thank you guys, really big help –  Jun 29 '16 at 17:47
  • Here, take a look: [Difference between id and name attributes in HTML](http://stackoverflow.com/questions/1397592/difference-between-id-and-name-attributes-in-html) – FirstOne Jun 29 '16 at 17:47
  • @Fred: Please do not close questions as duplicates of questions which they are clearly not. – Matt Jun 29 '16 at 19:45
  • @Shank You can use `` depending on your settings. – Rose Kunkel Jun 29 '16 at 20:27
  • @Fred-ii- I don't see the dupe link anymore, anyways I believe that the link was only targeting some of his errors. He has a lot more. – Shank Jun 29 '16 at 22:35
  • @Shank You're right, it was full of syntax errors, and not to mention it would never have worked because of the HTML form's and PHP position and a few other things. The HTML form and PHP's position took me a while to figure out why it wasn't working at all, for the error messages to come up in red. – Funk Forty Niner Jun 29 '16 at 23:10
  • @Matt Probably the first mistake I made in thousands. Just for the record though; I've seen quite a few questions in the past & some recent where much higher rep'd guys than me closed questions that hardly even or if not even addressed the real problem. I take it you want us all to keep an eye out for those and to flag them in order for them too to be told about it. TBH, the first I noticed was no name attribute and one not matching, which is why I closed it. There were other syntax errors which I've pointed out in an answer, and then some. I'll be more careful next time, but nobody's perfect. – Funk Forty Niner Jun 29 '16 at 23:27

1 Answers1

3

Ok. First off, nothing inside this if (isset($_POST['Submit'])){...} will ever happen, since you don't have a matching name attribute for it.

That alone would constitute as an Undefined index Submit notice.

Then you have missing braces for your conditionals, and those alone would constitute as parse errors.

Then you have name="Password" and $_POST['password'] another error there; letter-case and an Undefined index password notice.

  • You're not getting errors for them because you're not checking for them or your system isn't setup to catch them.

Also make sure that short open tags are set, otherwise change all <? to <?php or <? echo to <?=.

You also need to place your HTML form after the PHP in order for all this to work.

So in light of it all, here is your fixed code along with a few other minor mistakes you can look through that has been modified.

<?php
// For development purposes only
error_reporting(E_ALL);
ini_set('display_errors', 1);

$errorpass = "";
$errorusername = "";

if (isset($_POST['Submit']))
    {
    if (empty($_POST['Username']))
        {
        $errorusername = "name is required";
        }
      else
        {
        $Username = $_POST['Username'];

        if (!preg_match("/^[a-zA-Z ]*$/", $Username))
            {
            $errorusername = "Only numbers allowed";
            }

        }


    if (empty($_POST['Password']))
        {
        $errorpass = " A password is required";
        }
      else
        {
        $password = $_POST['Password'];
        if (!preg_match("/^[a-zA-Z ]*$/", $password))
            {
            $errorpass = "Need to contain numbers and symbols";
            }
        }

}
?>
<!DOCTYPE html>
<html>
   <head>
      <style>
         .error {
         color: red;
         }
      </style>
      <title>Register</title>
   </head>
   <body>
      <form action="#" method="POST">
         <h1 id="Signup">Sign Up</h1>
         <input type="text" name="Username" placeholder="Please Enter a Username">
         <span class="error"><? echo $errorusername;?></span>
         <br>
         <input type="password" name="Password" placeholder="Please Enter a Password">
         <span class="error"><? echo $errorpass;?></span>
         <br>
         <input type="Submit" id ="Submit" name ="Submit">
      </form>
   </body>
</html>

Footnotes:

This here

if (!preg_match("/^[a-zA-Z ]*$/", $Username))

for

$errorusername = "Only numbers allowed";

that preg_match checks for letters and not numbers.

Same for if (!preg_match("/^[a-zA-Z ]*$/", $password)) and $errorpass = "Need to contain numbers and symbols";

and is beyond the scope of this question. You will need to fix those yourself in order to get the proper syntax for what you want to check for.

  • I believe I have done more than enough here to get you going.
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141