still2blue
still2blue

Reputation: 193

Two part form validation in PHP

I am working on my first $_POST form. I have created a simple HTML form and used the post method and my action points to a php document. I want to do some validation with the php to make sure the passwords match and simple things like that. I guess I am not understanding how to make the form work for me because right now when I submit my form, all it does is show my php code on the next page. How do you get the php to actually check the values instead of just displaying the code? Here is what I have for my php file:

<?php

function validatePassword($pwd) {
    //create array to store test information
    $messages = [];
    //test for at least 8 characters
    if (strlen($pwd) < 8) {
        $messages []= "Your Password Must Contain At Least 8 Characters!<br />";
    }
    //test for max length
    if (strlen($pwd) > 16) {
        $messages []= "Your Password is too long!<br />";
    } 
    //test to see if password contains number
    if(!preg_match("#[0-9]+#", $pwd)) {
        $messages []= "Your Password Must Contain At Least 1 Number! <br />";
    }
    //test to see if password has capital letter
    if(!preg_match("#[A-Z]+#", $pwd)) {
        $messages []= "Your Password Must Contain At Least 1 Capital Letter!<br />";
    }
    //test to see if password has a lowercase letter
    if(!preg_match("#[a-z]+#", $pwd)) {
        $messages []= "Your Password Must Contain At Least 1 Lowercase Letter!<br />";
    }
    //test to see if password has special character
    if(!preg_match("#[^0-9A-Za-z]#", $pwd)) {
        $messages []= "Your Password Must Contain At Least 1 Special Character!<br />";
    }
    //test to see if password contains a space
    if (strpos($pwd, ' ') > 0) {
        $messages []= "Your password cannot contain a space!<br />";
    }
    //password passed all tests
    if (empty($messages)) {
        return "Password is acceptable<br />";
    }
    //return the array
    return implode("\n", $messages);
}

    if ($pass1 != $pass2){
         $msg = "Passwords do not match";
    }
    else{
        $msg = "Password confirmed!";
        }
    validatePassword($pass1);

?>

Form code:

<html>
<head>
<title>PHP Form</title>
</head>
<body>

<form name=newForm method=post action=formProcess.php>
UserName: <input type=text name=userName size=15 maxlength=15><br>
Password: <input type=password name=pass1 size=15><br>
Confirm Password: <input type=password name=pass2 size=15><br>
<p>
I agree to the terms and conditions.<br>
<input type=radio name=terms value=yes> Yes
<input type=radio name=terms value=no> No
<p>
Enter comments here:<br>
<textarea name=comments rows=6 cols=50 wrap=physical></textarea>
<p>
<input type=submit name=submitForm>
<input type=reset name resetForm>
</p>
</form>
</body>
</html>

By the way I know I can put the php in the HTML document, but I really want to attempt to do two seperate files and see how this works. Thanks for any help!

Upvotes: 2

Views: 405

Answers (3)

George Irimiciuc
George Irimiciuc

Reputation: 4633

It seems you don't have a web server

Download xampp and place your php file in the htdocs folder of the server, then you should be able to see it on http://localhost

Don't forget to actually start your Apache server and make sure it has a green light and no errors. Usually Skype will block it because it uses its port, so be careful on that.


Ok, first let's make some valid HTML

<html>

<head>
  <title>PHP Form</title>
</head>

<body>
  <form name="newForm" method="post" action="formProcess.php">UserName:
    <input type="text" name="userName" size="15" maxlength="15">
    <br>Password:
    <input type="password" name="pass1" size="15">
    <br>Confirm Password:
    <input type="password" name="pass2" size="15">
    <br>
    <p>I agree to the terms and conditions.
      <br>
      <input type="radio" name="terms" value="yes">Yes
      <input type="radio" name="terms" value="no">No
      <p>Enter comments here:
        <br>
        <textarea name="comments" rows="6" cols="50" wrap="physical"></textarea>
        <p>
          <input type="submit" name="submitForm">
          <input type="reset" name="resetForm">
        </p>
  </form>
</body>

</html>

Then in your formProcess.php file, delete everything and try something like

<?php
echo $_POST["userName"];

?>

If this doesn't print the value you submitted in your username field, then there is a problem with your server.

Upvotes: 1

Vimal Mishra
Vimal Mishra

Reputation: 1

In PHP there are two type validation such javascript validation (Client side validation) and another is Php Validation such as (Server side Validation).

1- In java Script validation done on Client Machine.

2- In Server Side (PHP validation) Done On server.

Upvotes: 0

gidim
gidim

Reputation: 2323

In order to run PHP pages you need to first install it with a web server. If you're using windows you can try WAMP which bundles PHP with Apache and MySQL: http://www.wampserver.com/en/

For Linux: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu

For MAC: https://www.mamp.info/en/

Upvotes: 0

Related Questions