Something wrong with isset($_POST) - php

I'm trying to make registration form, but there is some problem, I can't get where it is.
Errors:
Notice: Undefined index: username in ...
Notice: Undefined index: password in ...
<form method="POST">
<?php
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = md5($_POST['password']);
if(empty($username) or empty($password)) {
echo "<p>Fields are empty!</p>";
} else {
mysql_query("INSERT INTO users VALUES('', '$username', '$password', '2', '')");
echo "<p>Registration successful!</p>";
}
}
?>
<p><label for="username">Username: </label><input type="text" id="username" /></p>
<p><label for="password">Password: </label><input type="password" id="password" /></p>
<input type="submit" name="submit" value="Sign up">
</form>
Thanks!

You need to put the name for your inputs:
<label for="username">Username: </label><input type="text" name="username" id="username" />
<label for="password">Password: </label><input type="password" name="password" id="password" />
That should fix the problem!

Related

Data won't pass into Database (PHP)

My form won't post inserted data into my database, i know this is a very basic problem but I am only just starting to learn to code
connect_to_mysql.php:
<?php
$db_host="localhost";
$db_username="ajamesbird";
$db_pass="";
$db_name="test";
$db_connect = mysql_connect("$db_host","$db_username","$db_pass")or die
("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
login.php
<html>
<?php include "C:\Users\andrew\Documents\Websites\Seller\storescripts\connect_to_mysql.php";?>
<?php
if(isset($_POST['loginform'])){
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$sql = ("INSERT INTO users (id, access_level, username, firstname,
lastname, email, password, dob, date_added, activated)
VALUES ('NULL','NULL','$username','$firstname','$lastname','$email', '$password', '$dob', now(), '0')") or die (mysql_error());
if(!mysql_query($db_connect, $sql)){
die('Error inserting into database');
}
}
?>
<head>
<link href="style/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="login.php" enctype="multipart/form-data" name="loginform" id="loginform" method="post">
<input name="username" type="text" id="username" size="63" class="form-control" value="Username" required/>
<input name="firstname" type="text" id="firstname" size="63" class="form-control" value="First name" required/>
<input name="lastname" type="text" id="lastname" size="63" class="form-control" value="Last name" required/>
<input name="email" type="email" id="email" size="63" class="form-control" value="Email" required/>
<input name="password" type="password" id="password" size="63" class="form-control" value="Password" required/>
<input name="dob" type="text" id="dob" size="63" class="form-control" value="Date of Birth" required/>
<input type="submit" name="button" id="button" size="64" value="Sign Up" />
</form>
</body>
</html>
Thank you in advance
Try to move name="loginform" from and put it in hidden input
<html>
<?php include "C:\Users\andrew\Documents\Websites\Seller\storescripts\connect_to_mysql.php";?>
<?php
if(isset($_POST['loginform'])){
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$sql = ("INSERT INTO users (id, access_level, username, firstname,
lastname, email, password, dob, date_added, activated)
VALUES ('NULL','NULL','$username','$firstname','$lastname','$email', '$password', '$dob', now(), '0')") or die (mysql_error());
if(!mysql_query($db_connect, $sql)){
die('Error inserting into database');
}
}
?>
<head>
<link href="style/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="login.php" enctype="multipart/form-data" method="post">
<input name="username" type="text" id="username" size="63" class="form-control" value="Username" required/>
<input name="firstname" type="text" id="firstname" size="63" class="form-control" value="First name" required/>
<input name="lastname" type="text" id="lastname" size="63" class="form-control" value="Last name" required/>
<input name="email" type="email" id="email" size="63" class="form-control" value="Email" required/>
<input name="password" type="password" id="password" size="63" class="form-control" value="Password" required/>
<input name="dob" type="text" id="dob" size="63" class="form-control" value="Date of Birth" required/>
<input type="submit" name="button" id="button" size="64" value="Sign Up" />
<input type="hidden" name="loginform">
</form>
</body>
</html>

$_POST variable contains no data

I'm having a basic form like this:
<form method="post" action="register.php" class="form">
<input id="a" type="text" placeholder="Cod acces" name="access-code" size="20" required /><br>
<input id="b" type="password" placeholder="Parola" name="password" autocomplete="new-password" size="20" required /><br>
<input id="c" type="password" placeholder="Confirma parola" name="re-password" autocomplete="new-password" size="20" required /> <br><br>
<input type="submit" value="Register" name="register" />
</form>
In register.php i have the following 3 lines of code:
$password = $_POST["password"];
$repassword = $_POST["re-password"];
$acces_code = $_POST["access-code"];
Even if this code is as simple as it looks, my $_POST variable is empty. Even weirder, if I press F12 to see the request data, all variables and it's values are there.
I'm using XAMPP on Windows.
Here is the code I'm also with Windows version 10 and Xampp Server.
In your index.php paste this code below
<html>
<body>
<form method="post" action="register.php" class="form">
<input id="a" type="text" placeholder="Cod acces" name="access-code" size="20" required /><br>
<input id="b" type="password" placeholder="Parola" name="password" autocomplete="new-password" size="20" required /><br>
<input id="c" type="password" placeholder="Confirma parola" name="re-password" autocomplete="new-password" size="20" required /> <br><br>
<input type="submit" value="Register" name="register" />
</form>
</body>
</html>
and then in your register.php paste this code below
<?php
if(isset($_POST['register'])){
echo $password = $_POST["password"]." ";
echo $repassword = $_POST["re-password"]." ";
echo $acces_code = $_POST["access-code"]." ";
}
Successfully getting and outputting your input on the form. Hope this will help you

echo is not showing anything on output browser side

This should be printed on output browser side:
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
But there is nothing.
This is my full code, which I should have added in the first place.
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2) {
if($pass1 == $pass2) {
//All good. Nastavi broo.
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($email1);
$email2 = mysql_escape_string($email2);
$pass1 = mysql_escape_string($pass1);
$pass2 = mysql_escape_string($pass2);
}else{
echo "Sorry, your password is not corrext.";
exit();
}
}else{
echo "Sorry!";
}
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
}
?>
Your form will only appear once you click the submit button because it is set inside the if(isset($_POST['submit'])){...} conditional statement. Therefore, you need to move your last brace } above $form = <<<EOT.
} // it belongs here instead of below echo $form;
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
?>
and make sure you are accessing it as http://localhost/file.php and not as c://file.php
EDIT:
Here's a rewrite:
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2) {
if($pass1 == $pass2) {
//All good. Nastavi broo.
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($email1);
$email2 = mysql_escape_string($email2);
$pass1 = mysql_escape_string($pass1);
$pass2 = mysql_escape_string($pass2);
}else{
echo "Sorry, your password is not corrext.";
exit();
}
}else{
echo "Sorry!";
}
} // brace for submit conditional
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
?>
Passwords
I also noticed that you may be storing passwords in plain text. This is not recommended. If you intend on going LIVE with this, do read the following.
Use one of the following:
CRYPT_BLOWFISH
crypt()
bcrypt()
scrypt()
On OPENWALL
PBKDF2
PBKDF2 on PHP.net
PHP 5.5's password_hash() function.
Compatibility pack (if PHP < 5.5) https://github.com/ircmaxell/password_compat/
Other links:
PBKDF2 For PHP

PHP $_POST getting undefined index, even though inputs are given

I am trying to get a username and password from an HTML form using $_POST method,
but i am getting an undefined index error with given input.
the form portion of my index file is as follows
<form class="form-signin" role="form"action="" method="post">
<h2 class="form-signin-heading">title<em class='current'>title</em></h2>
<input id="username" type="username" class="form-control" placeholder="Username" required autofocus>
<input id="password" type="password" class="form-control" placeholder="Password" required>
<input name ="submit" type="submit" value="sign in">
</form>
i also have my php script (called auth.php) included in the top of my index file.
this is my auth.php script code sample
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else{
echo "YAY";
}
}
?>
The "username or password is empty" statement keeps getting printed, even when I enter in a username and password into the input fields. What is the problem
Missing "name" attribute in input fields, it should be
EDIT 2 : Missing type="text" in username input
<input type="text" name='username' id="username" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
Correct your form , 1. Name missing, 2. No datatype with the name of username
<form class="form-signin" role="form" action="" name="loginform" method="post">
<h2 class="form-signin-heading">title<em class='current'>title</em></h2>
<input id="username" name="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input id="password" name="password" type="password" class="form-control" placeholder="Password" required>
<input name="submit" type="submit" value="sign in">
</form>
<input id="username" name="username" type="username" class="form-control" placeholder="Username" required autofocus>
<input id="password" type="password" name="password" class="form-control" placeholder="Password" required>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else if(empty($_POST['username']){
echo "Username is empty";
}
else if(empty($_POST['password']){
echo "Password is empty";
}
else{
echo "YAY";
}
}
?>
input type = text and name attr required
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
Check values using print_r() or var_dump if you are not getting the desired output.
This will help you to check which part of your code malfunctions. If you have done so, you would have found that there's something wrong only in your html form content, as you wont be having any field content printed and you would have easily sorted it out...
Set the username input's type to "text":
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>

Form not POSTing values [duplicate]

This question already has answers here:
$_POST is empty after form submit
(6 answers)
Closed 6 months ago.
Form:
<form id="register" method="POST" action="pro/register.php">
<input type="text" maxlength="30" placeholder="Username" id="user" /><br />
<input type="email" maxlength="64" placeholder="Email" id="email" /><br />
<input type="password" placeholder="Password" id="pass1" /><br />
<input type="password" placeholder="Confirm Password" id="pass2" /><br />
<input type="submit" value="Register" id="submit_register" />
</form>
pro/register.php page:
$user = $_POST['user'];
$email = $_POST['email'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
//Debug only
echo "<strong>Details:</strong><br>";
echo $user.", ".$email.", ".$pass1.", ".$pass2."<br>";
if($pass1!==$pass2){
header('Location:../login.php?alert=pass');
exit;
}
$hash = hash('sha256', $pass);
include "../inc/functions.php";
$salt = createSalt();
$hash = hash('sha256', $salt . $hash);
include "../inc/connect.php";
$stmt = $dbh->prepare("INSERT INTO `users`
(`username`,`email`,`password`,`salt`,`pic`)
VALUES (:username,:email,:password,:salt,:pic)");
$stmt->bindParam(':username',$user);
$stmt->bindParam(':email',$email);
$stmt->bindParam(':password',$hash);
$stmt->bindParam(':salt',$salt);
$stmt->bindParam(':pic',$pic);
$stmt->execute();
$dbh=NULL;
header('Location:../login.php?alert=newreg');
Output when form is posted:
Details:
, , ,
<input type="text" maxlength="30" placeholder="Username" id="user" name="user"/><br />
try adding the name field.
You must include the 'name' attribute in your form inputs, this is what determines where the value goes in $_POST.

Categories