2

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!

still2blue
  • 193
  • 1
  • 18
  • _How do you get the php to actually check the values instead of just displaying the code?_ - Install php – Federkun Sep 24 '15 at 17:14
  • @Leggendario what do you mean install php? – still2blue Sep 24 '15 at 17:16
  • what is $pass1 and what is $pass2??? you have to echo something here...if validation fails...becz funtion is just returning values it will not echo anything... – Akshay Sep 24 '15 at 17:21
  • They are from the HTML form when the user enters a password and confirms it. I didn't echo anything yet because I was more concerned with why after the user submits the form, all I see is my php code. – still2blue Sep 24 '15 at 17:23
  • Per the answers below; If you already have a server installed like xampp or wampp, then maybe apache/nginx is not installed ? Try creating a separate php file and add this: and then run it inside your browser to see what happens `` – yardie Sep 24 '15 at 17:38
  • Returns PHP Version 5.4.44-0+deb7u1 with a big list of info – still2blue Sep 24 '15 at 17:39

3 Answers3

1

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.

George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88
  • I do have a server. I have uploaded the html and php to the server. When I submit my form, all I see is exactly what I posted above. – still2blue Sep 24 '15 at 17:22
  • You sure your file extension is .php? – George Irimiciuc Sep 24 '15 at 17:23
  • Yes. My html uses post and action = myphpfile.php. After the user hits submit, all I see is my code. If i do a print_r $_POST I see all the values in the array – still2blue Sep 24 '15 at 17:24
  • Mind showing your form code, too? For some reason, your php code is not executed. You shouldn't see php code as plaintext. – George Irimiciuc Sep 24 '15 at 17:30
  • Can you try doing echo on one Post variable like I said and let's see what happens? – George Irimiciuc Sep 24 '15 at 17:42
  • I tried and it still prints plaintext. This is so weird. I know php is installed on the server and `` confirmed that. What else could be the issue? – still2blue Sep 24 '15 at 17:44
  • There is definitely something wrong with your server. Could you try [this](http://stackoverflow.com/questions/3555681/why-are-my-php-files-showing-as-plain-text) or [this](http://serverfault.com/questions/523131/php5-is-installed-but-apache-is-displaying-php-as-uninterpreted-text-how-can-i) ? Don't forget to restart your server. If not, uninstall and reinstall your application and try again. Maybe you unticked something at the installation part or you installed a wrong version. – George Irimiciuc Sep 24 '15 at 17:48
  • Restart did it! Thank you so much for all your help. Answer accepted – still2blue Sep 24 '15 at 17:51
0

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/

gidim
  • 2,314
  • 20
  • 23
0

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.