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

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>

Related

$_post function seems not working

<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" value="">
<br>
Password :
<input type="password" id="password" class="password" name="password" value="">
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" value="">
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" value="">
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_REQUEST['submit']))
{
$res = "insert into register(username, password, phoneno) value('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
if(!empty($_POST)){
echo "Registered!";
}
else {
echo "Try Again!";
}
}
?>
I want to check all $_Post fields check for empty, null or 0 if values of form is not filled show error "try again" and filled show error "registered"? please tell me first is this function is correct or not. if not correct what the correct function.
If you want to validate fields through php, you have to check each field individually. There are some checks you can apply on a data.
if ($_POST['field'] === null) //I would suggest === because it will return false if $_POST['field'] is 0 or empty string
if (empty($_POST['field'])) //to check empty data
if (isset($_POST['field'])) //to check if data exists
If you want to validate your fields like is either string? or digits? or alpha numeric? or all lowercase? or uppercase? There are certain functions which allows you to check field by just calling a single function
Please have a look at this page: CType Functions
You can also use this option. Make all fields required you want to check null or 0, this will make sure that data in field is must before submitting form.
Try changing input field from:
<input type="text" id="username" class="username" name="username" value="">
to:
<input type="text" id="username" class="username" name="username" value="" required >
Also as Rishi said you should also edit query, change values to values
Also 1 tip, you are inserting data in database first and then validating data, according to me it is not good practice, you should first validate data then if all went good then insert it into database, else you will add bad data in database also.
Your final code will be:
<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" required >
<br>
Password :
<input type="password" id="password" class="password" name="password" required >
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" required >
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" required >
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_POST['submit']))
{
$res = "insert into register(username, password, phoneno) values('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
}
?>

Bootstrap forms: it doesn't retrive POST data. With GET it does

Implementing forms with bootstrap's classes, in a first page I wrote this code
<form action="dologin.php" method="post">
<input type="text" name="email" class="form-control" placeholder="Username">
<input type="password" name="password" class="form-control" placeholder="Password">
<input type="image" src="img/login.png" alt="Login">
</form>
and in dologin.php I tried to retrive the data in this way
$email = $_POST['email'];
echo $email;
It doesn't work, it doesn't print anything.
But if I use the get method, in first page:
<form action="dologin.php" method="get">
<input type="text" name="email" class="form-control" placeholder="Username">
<input type="password" name="password" class="form-control" placeholder="Password">
<input type="image" src="img/login.png" alt="Login">
</form>
In dologin.php
$email = $_GET['email'];
echo $email;
It works printing what was input in the form.
Thank you for helping.
I believe it has something to do with the "image" input.
have you considered using a button element instead?
<button type="submit" name="someName" value="someValue"><img src="someImage.png" alt="SomeAlternateText"></button>
Try this :-
<form action="dologin.php" method="post">
<input type="text" name="email" class="form-control" placeholder="Username">
<input type="password" name="password" class="form-control" placeholder="Password">
<input type="image" src="img/login.png" type="submit" alt="Login">
</form>
And in dologin.php :
email = $_POST['email'];
echo $email;

Why are my form variables not passing through POST?

I am sending form data through POST, but the corresponding POST variables are not set, and do not.
Also, when I store POST data into local PHP variables, I seem to be unable to use those variables. (Once I resolve the first issue, I have a feeling I will be able to user the variables too.)
My error messages output by the second page (see below) is:
Notice: Undefined variable: postUsername in (...somepath)\scripts\create-member.php on line 10
(Form page) :
<form action="scripts/create-member.php" method="POST">
<input type="text" name"username" value="" placeholder="User Name"> <br />
<input type="password" name"password" value="" placeholder="Password"> <br />
<input type="password" name"passwordConfirm" value="" placeholder="Confirm Password"> <br />
<!-- ?type email or type text -->
<input type="email" name"email" value="" placeholder="Email" autofocus> <br />
<input type="submit" name="submitRegistration" value="Register!">
</form>
(Second page) scripts\create-member.php:
<?php
//!proper way to declare variables obtained from POST.
// Data from form "register.php"
if ( isset($_POST['username']) ) {
$postUsername = $_POST['username'];
}
echo $postUsername; // <-- this is line 10
?>
I've tried using isset() for the submit button too, but that didn't solve the problem.
I've simplified the code by a lot here, and ran it testing it too.
In your html code, you have missed = for name
name="username"
Instead of name"username"
Here's your fixed code.
<input type="text" name="username" value="" placeholder="User Name"> <br />
<input type="password" name="password" value="" placeholder="Password"> <br />
<input type="password" name="passwordConfirm" value="" placeholder="Confirm Password"> <br />
<!-- ?type email or type text -->
<input type="email" name="email" value="" placeholder="Email" autofocus> <br />
the ploblem is not at register.php
you can try to write like this:
if ( isset($_POST['username']) ) {
$postUsername = $_POST['username'];
echo $postUsername;
}
I had a similar problem posting to an index.php file in a folder.
<form id="register" name="register" method="POST" action="/register">
// BROKEN
all $_POST vars were empty until I added a trailing forward slash to the form post action
<form id="register" name="register" method="POST" action="/register/">
// WORKS

PHP isset function for submit check not working

I have just discovered that the isset function is no longer working on my login and register forms. Very strange I though, so I undid everything that I had recently done to see if it was causing it but no luck. If i remove the isset and replace with this;
if($_SERVER['REQUEST_METHOD'] == "POST"){
it works! But I have two forms on one page so I need to check which one is submitted.
Here's the isset function:
if (isset($_POST['submit_login'])) {
And the submit button just so you know it has the correct name;
<input type="submit" name="submit_login" value="Login" class="buttonClassic"/>
The one for the register form is exactly the same but with name submit_reg.
Form:
<form action="<?php echo htmlentities('Login'); ?>" method="post" id="login">
<p class="p1">Already signed up? Log in</p><hr/>
<label for="email">Your email address </label><input type="email" required name="email" placeholder="Email" class="text" id="email">
<label for="password">Your password </label><input type="password" name="pass" required placeholder="Password" class="text" id="password">
<center><input type="submit" name="submit_login" value="Login" class="buttonClassic"/></center>
<div class="center-align-text">
<p class="p3">Forgotten your password?</p>
</div>
</form>
reg form:
<form action="<?php echo htmlentities('Login'); ?>" method="post" id="register" >
<p class="p1">New to NBS? Sign up, it's free!</p><hr/>
<label for="reg_email">What's your email address? </label><input type="email" name="email" required placeholder="Email" class="text" id="reg_email">
<label for="reg_password">Choose a password </label><input type="password" required name="pass" placeholder="Password" class="text" id="reg_password">
<label for="reg_password2">Re-type password </label><input type="password" required name="pass2" placeholder="Re-type password" class="text" id="reg_password2">
<input type="checkbox" name="subscribed" value="subscribed" id="subscribed"><label for="subscribed">Yes, send me email updates from NewBorn Sounds. </label>
<br/>
<input type="checkbox" required="flag" name="terms" value="ticked" id="terms"><label for="terms">I agree to the terms & conditions.</label>
<center><input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic"></center>
</form>
If you need anything more just shout!
Oh and I know I could just submit the form to an external PHP script but I don't particularly want to do that as I would like the user input errors to be outputted to the same page. I know I could just use ajax, which I do, but I am trying to keep javascript as an add-on and not reduce the user experience for no js.
Full HTML:
<div id="login_form_wrapper">
<form action="Login" method="post" id="login" novalidate="novalidate">
<p class="p1">Already signed up? Log in</p><hr>
<label for="email">Your email address </label><input type="email" required="" name="email" placeholder="Email" class="text" id="email">
<label for="password">Your password </label><input type="password" name="pass" required="" placeholder="Password" class="text" id="password">
<center><input type="submit" name="submit_login" value="Login" class="buttonClassic"></center>
<div class="center-align-text">
<p class="p3">Forgotten your password?</p>
</div>
</form>
<form action="Login" method="post" id="register" novalidate="novalidate">
<p class="p1">New to NBS? Sign up, it's free!</p><hr>
<label for="reg_email">What's your email address? </label><input type="email" name="email" required="" placeholder="Email" class="text" id="reg_email">
<label for="reg_password">Choose a password </label><input type="password" required="" name="pass" placeholder="Password" class="text" id="reg_password">
<label for="reg_password2">Re-type password </label><input type="password" required="" name="pass2" placeholder="Re-type password" class="text" id="reg_password2">
<input type="checkbox" name="subscribed" value="subscribed" id="subscribed"><label for="subscribed">Yes, send me email updates from NewBorn Sounds. </label>
<br>
<input type="checkbox" required="flag" name="terms" value="ticked" id="terms"><label for="terms">I agree to the terms & conditions.</label>
<center><input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic"></center>
</form>
</div>
maybe you could do something like this :
if($_SERVER['REQUEST_METHOD'] == "POST"){
if (isset($_POST['submit_login'])) {
//do something
} else {
//do something else
}
}
Form action needs to be a valid url, e.g. "/login.php".
Checkboxes have either the value given (once checked), or they do not appear at all. Best is to double check them: "isset($_POST['mycheckbox']) && 'value' ==$_POST['mycheckbox']".
Show us the php you use to evaluate the form.
How this?
<input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic">
<?php
if($_POST['submit'] == 'Sign Up'){
//do something
}
?>

Method POST not retrieveing data from form.

I am currently learning Web-Design(HTML,PHP,Javascript) and I have created my own site a file called MySite.html.
I have a signin screen which uses POST method to send data to a file called signin.php.
For now it has a simple check which confirms whether data is inputted or not.
But when I enter the Username and Password, the script tells me that I haven't inputted anything.
Here is the code----->
<form name="signin" action="signin.php" method="POST">
<p><input type="text" id="username" placeholder="Enter Username"></p>
<p>
<input type="password" id="pass" placeholder="Password">
<input type="submit" id="sgnin" value="Sign In">
</p>
<p id="small">
<input type="checkbox" id="rem">Remember me.Forgot password?
</p>
<span><p> </p>
</form>
The php script--->
<?php
if(isset($_POST['username']) && isset($_POST['pass']) && !empty($_POST['username']) && !empty($_POST['pass'])){
echo 'Logged In';
}else die("enter something");
?>
Plz Help!
You are not giving name attribute to your form elements give it like this
<input type="text" id="username" placeholder="Enter Username" name="username">
and
<input type="password" id="pass" placeholder="Password" name="pass">

Categories