Josel Parayno Josel Parayno - 5 months ago 50
PHP Question

php mysql forms validation

i already got message in my browser

Notice: Undefined variable: password in C:\xampp\htdocs\udemy\mysql\login.php on line 8 pls insert username and password

even i haven't submitted the form.

One more question how can i validate if the username and the password are submit with text.


$username = $_POST['username'];
$password = $_POST['password'];

if($username && $password){
echo $username;

echo $password;
echo "pls insert username and password";


<!DOCTYPE html>
<link rel="stylesheet"
<div class="container">

<div class="col-sm-6">
<form action="login.php" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username"

<div class="form-group">
<label for="username">Password</label>
<input type="password" name="password"

<div class="form-group">
<input type="submit" name="submit"
class="btn btn-primary">



Answer Source

Declare $username=$password='' before if(isset($_POST['submit']))

Maybe this could help you. Do modify in this code , but from this code you will get your error answer and also validation for you `



if (empty($_POST["username"])) {

      else {
        $username = test_input($_POST["username"]);

        if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
          echo 'User name only suppost space and letter';

       if (empty($_POST["password"])) {
        echo 'password can't be empty;
      } else {
        $password = test_input($_POST["password"]);





            $sql = "INSERT INTO tbl_user (id,username,password) VALUES 




function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download