Why are my form variables not passing through POST? - php

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

Related

$_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

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;

How to send details to a php page?

How do I send information from a HTML page to a php page which then makes a connection to a database?
This is my code so far on my website: http://jsfiddle.net/xiiJaMiiE/wNraL/
<h1>
<div id=signin>
<input type="text" name="textbox1" value="Username" onfocus="if
(this.value==this.defaultValue) this.value='';" size="19%" height="50"/></br>
<input type="password" name="password" value="Password" onfocus="if
(this.value==this.defaultValue) this.value='';" size="19%" height="50"/>
</div>
</h1>
Thanks in advance!
Replace your h1 element (you don't have a heading so you shouldn't say you do) with a form element.
Give it an action attribute that specifies the URL of the PHP program you want to submit the data to.
Since you are sending authentication data, add a method attribute that to POST the data.
<form action="signin.php" method="POST">
<div id=signin>
<label>
Username
<input type="text" name="username">
</label>
<label>
Password
<input type="password" name="password">
</label>
</div>
</form>
The data will then be available to PHP via $_POST['field_name_here'].
<div id=signin>
<form action="connection.php" method="POST">
<input type="text" name="username" value="Username" onfocus="if (this.value==this.defaultValue) this.value='';" size="19%" height="50"/>
<br>
<input type="password" name="password" value="Password" onfocus="if (this.value==this.defaultValue) this.value='';" size="19%" height="50"/>
</form>
</div>
You can access these passed variables in the conneciton.php file with $_POST['username'] and $_POST['password'].
you should set the action attribute to a file that will process form data (eg process. php) and pass form data to your database using $_POST('filedName') on submitting the form.

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
}
?>

How to store html form data in a cookie so it's still there after the form is submitted

I have made this html form, it submits all the information to a MySQL database. Pretty simple stuff.
How do I store the username in a cookie so that once it's entered? I would like to ensure that once the user enters their username once, it pre-populates the username field in the future.
<!-- Message board submissionform -->
<form id="frmMB" name="frmMB" action="insert.php" method="post" enctype="multipart/form-data">
<label class="name"><input name="name" placeholder="Enter your name" type="text" id="name" onFocus="if(this.value=='Enter your name'){this.value=''};" onBlur="if (this.value==''){this.value='Enter your name'};" value="Enter your name" size="80" maxlength="10" ></label>
<br />
<label class="message">
<input name="post" placeholder="Enter a message" type="text" id="post" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;" value="Enter a message" size="80" maxlength="140" data-maxsize="3">
</label>
<br />
<label>
<input name="Submit" class="large button" type="submit" value="submit">
</label>
I think something like this would just do this trick.
setcookie($_POST['name'], $value, time()+3600);
if(isset($_COOKIE['name']) && !empty($_COOKIE['name']))
{
// do some stuff here because the name is set in the cookie.
}
First you need to start a session -> session_start()
Once done so, you have created a public $_SESSION array and can add elements to it. Easiest way to do so is:
<?php
if(isset($_POST['username'])){
sesion_start();
$_SESSION['username'] = $_POST['username'];
}
...
Cheers!

Categories