$_POST variable contains no data - php

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

Related

Second submit button not working in php

I have been having problems with the second button not running like the first button. this is the code I have:
<p>
<form method="POST">
<input placeholder="Username" type="text" name="username"><br /><br />
<input placeholder="password" type="password" name="password"><br /><br />
<input value="Login" type="submit" name="log_In">
</form>
</p>
</div>
<?php
if(isset($_POST['log_In'])) {
#$f_name = $_POST['fname'];
#$s_name = $_POST['sname'];
#$stud_Id = $_POST['studId'];
#$uname = $_POST['uname'];
#$pass = $_POST['pass'];
#$rpass = $_POST['rpass'];
#$email = $_POST['email'];
#$remail = $_POST['remail'];
#var_dump($f_name);
header("Location:home.php");
}
?>
</div>
<div align="right">
<div>
<p>
<h2>Sign Up</h2>
</p>
<p>
<form>
<input placeholder="Forename" type="text" name="fname" id="Forename"><br /><br />
<input placeholder="Surname" type="text" name="sname"><br /><br />
<input placeholder="Student Id" type="text" name="studId"><br /><br />
<input placeholder="Username" type="text" name="uname"><br /><br />
<input placeholder="password" type="password" name="pass" min="6" max="32"><br /><br />
<input placeholder="Re-type password" type="password" name="rpass" min="6" max="32"><br /><br />
<input placeholder="Email" type="" name="email"><br /><br />
<input placeholder="Re-type Email" type="remail" name="remail"><br /><br />
<input value="Sign Up" type="submit" name="sign_Up">
</form>
</p>
</div>
<?php
if(isset($_POST['sign_Up'])) {
header("Location:home.php");
}
?>
</div>
"if(isset($_POST['sign_up'])) {" is not being run and is just refreshing the page and removing all items from the form.
thanks
By default <form> method is GET. So if(isset($_POST['sign_Up'])) won't work. Change it to if(isset($_GET['sign_Up'])).
Or change your second form tag to:
<form method="POST">
Remember not to use header function after generating HTML content, move it to top!
header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
So it will be better if it is like this:
<?php
if(isset($_POST['log_In']) || isset($_POST['sign_Up'])) {
header("Location:home.php");
}
?>
<form method="POST">
<input placeholder="Username" type="text" name="username">
<br />
<br />
<input placeholder="password" type="password" name="password">
<br />
<br />
<input value="Login" type="submit" name="log_In">
</form>
<div align="right">
<div>
<p>
<h2>Sign Up</h2>
</p>
<p>
<form method="post">
<input placeholder="Forename" type="text" name="fname" id="Forename">
<br />
<br />
<input placeholder="Surname" type="text" name="sname">
<br />
<br />
<input placeholder="Student Id" type="text" name="studId">
<br />
<br />
<input placeholder="Username" type="text" name="uname">
<br />
<br />
<input placeholder="password" type="password" name="pass" min="6" max="32">
<br />
<br />
<input placeholder="Re-type password" type="password" name="rpass" min="6" max="32">
<br />
<br />
<input placeholder="Email" type="" name="email">
<br />
<br />
<input placeholder="Re-type Email" type="remail" name="remail">
<br />
<br />
<input value="Sign Up" type="submit" name="sign_Up">
</form>
</p>
</div>
</div>
You forgot to add method="post"
<form method="post">
<input placeholder="Forename" type="text" name="fname" id="Forename"><br /><br />
<input placeholder="Surname" type="text" name="sname"><br /><br />
<input placeholder="Student Id" type="text" name="studId"><br /><br />
<input placeholder="Username" type="text" name="uname"><br /><br />
<input placeholder="password" type="password" name="pass" min="6" max="32"><br /><br />
<input placeholder="Re-type password" type="password" name="rpass" min="6" max="32"><br /><br />
<input placeholder="Email" type="" name="email"><br /><br />
<input placeholder="Re-type Email" type="remail" name="remail"><br /><br />
<input value="Sign Up" type="submit" name="sign_Up">
</form>
in second form you have not define the method if method is not defined it will accept the GET method default so change your second form tag by
<form action="" method="POST">

How to pass a value and echo it within a form

hello i have a page where users can register, and so after registeration i want to pass the username and password from my registeration form to another form on a next page, mean while this form on the next page should hold username and password values that are passed from the first page.
the form is echoed correctly but the values dont come correctly.
this is what i ahve tried
echo '<form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="one" id="one">
<input type="text" placeholder="Username" name="Username" value="<?php echo $_POST["Username"]; ?>" >
<p> </p>
<input type="password" placeholder="Password" name="Password" value="<?php echo $_POST["Password"]; ?>">
<br>
<input name="Submit" type="submit" class="btn" value="Sign In" />
</form>';
but the values dont come out, is there a right way to echoe this thanks
echo '<form ACTION="'.$loginFormAction.'" METHOD="POST" name="one" id="one">
<input type="text" placeholder="Username" name="Username" value="'.$_POST["Username"].'" >
<p> </p>
<input type="password" placeholder="Password" name="Password" value=".'$_POST["Password"].'">
<br>
<input name="Submit" type="submit" class="btn" value="Sign In" />
</form>';
Try This And Reply
You don't do this with echo, because the echo is just some HTML on the other page. You can also save them.
What you want is a form most likely:
Page 1
<form action="page2.php" method="post">
Name: <input type="text" name="username" /><br />
<input type="submit" name="submit" value="Send" />
</form>
Page2
<?php
echo "Your sent username: ", $_POST['username'];
?>

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

Login form not authenticating

I have created a login form for my website but currently, it is not working and I am being directed to invalid.php which shows my authentication has failed.
<?php
session_start();
include("scripts/dbconnect.php");
$numrows=0;
$password=$_POST['password'];
$email=$_POST['email'];
$query="select fname,lname,email from mayan_users where (password='$password' && email='$email')";
$link = mysql_query($query);
if (!$link) {
die('login error');
}
$numrows=mysql_num_rows($link);
if ($numrows>0){ // authentication is successfull
$row = mysql_fetch_array($link, MYSQL_ASSOC);
$_SESSION['user']['fname']=$row['fname'];
$_SESSION['user']['lname']=$row['lname'];
$_SESSION['user']['email']=$row['email'];
header("location:../index2.php");
} else {
header("location:../invalid.php"); // authentication was unsuccessfull
}
?>
and here is my login form
<div id="Loginform" style="background-color:fuchsia; width:100%">
<span id="logspan">
<input type="email" name="email" id="email" placeholder="Email" required />
<input type="password" name="password" id="password" placeholder="Password" required/>
<input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
</span>
</div>
Your login may need to be to be wrapped in form tags if you aren't submitting it via some other method.
<form method="post" action="">
<span id="logspan">
<input type="email" name="email" id="email" placeholder="Email" required />
<input type="password" name="password" id="password" placeholder="Password" required/>
<input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
</span>
</form>
If your PHP is in another document then set the action to the form processor
action="form-processor.php"
(Thanks njk!)
You don't have the form tags.Add them like this
<form method="POST" action="">
<!-- INSERT INPUTS HERE -->
</form>
Try this:
<form id="Loginform" style="background-color:fuchsia; width:100%">
<span id="logspan">
<input type="email" name="email" id="email" placeholder="Email" required />
<input type="password" name="password" id="password" placeholder="Password" required/>
<input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
</span>
</form>
First of all, I suggest you to use PDO if your PHP version support it.
Then I suppose that your javascript function sends in the correct way the parameters.
Finally, you could avoid to check the number of rows. Simply
if($row = mysql_fetch_array($link))
{
...
}
else
{
header("location: ../invalid.php");
exit; //It's a good practice
}

Categories