/    Sign up×
Community /Pin to ProfileBookmark

$_POST not working

trying to recall a variable in two different PHP files, The first is the username and the other password. Theses were saved as:
$username = $_POST[“user”];
$password = $_
POST[“pass”];
‘user’ and ‘pass’ was the input tag name for the user to enter username and password this works fine when searching the database for the user. the problem is were i am trying to recall these variables to select all the data in the database in my settings file. My Login form PHP is:
<?php

session_start();
$host=”localhost”;
$dbuser=”root”;
$dbpassword=””;
$db=”mydb”;

$conn = mysqli_connect($host, $dbuser,$dbpassword);
mysqli_select_db($conn, $db);
if(isset($_POST[“login”]))
$username = $_
POST[“user”];
$password = $_POST[“pass”];
$sql = mysqli_query($conn, “SELECT username, password FROM users WHERE username =’$username’AND password=’$password’ limit 1”);
if(mysqli_num_rows($sql)==1){
header(“location: DailyPlanner.php”);
}
else{
echo ‘<script>alert(“Wrong Login Details. Try Again!”)</script>’;
}
}
?>

And my settings file is:
<?php
session_start();
$host=”localhost”;
$dbuser=”root”;
$dbpassword=””;
$db=”mydb”;
$conn = mysqli_connect($host,$dbuser,$dbpassword);
mysqli_select_db($conn, $db);
$_POST[‘user’] = $username;
$_
POST[‘pass’] = $password;
?>
Just says the variable is undefined and have researched a tonne and cant find a solution any help is appreciated.

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmOct 25.2018 — $POST ?? Did you actually try and run any script with this reference in it? Or did you type your supposed code into the forum page INSTEAD of copy and pasting it in here to be sure you gave us an accurate view of your code?
Copy linkTweet thisAlerts:
@ASHR48authorOct 25.2018 — Yeah sorry don't know why is came out like that i have run it and it is $_POST. Just comes up with undefined variable.
Copy linkTweet thisAlerts:
@ASHR48authorOct 25.2018 — All my code works even excluding the $_POST, just can't seem to get the $_POST to work
Copy linkTweet thisAlerts:
@ginerjmOct 25.2018 — So - cut and paste the specific area that does not work including the parts that we need to see in order to make sense of the first part. That would be the form code and the php that process the input and of course the line or lines that actually are failing you.

You have done some echo-ing of values to ensure that you are debugging this properly I hope.
Copy linkTweet thisAlerts:
@ASHR48authorOct 25.2018 — @ginerjm#1597116

Login Form:

<form method="POST" action="Protocol2.php">

<div id="input">

Username:<input type="text" placeholder="Enter Username" name="user" required>

Password:<input type="password" placeholder="Enter Password" name="pass" required>

</div>

<button type="submit" name="login">Login</button>

</form>

The Protocol2 Which searches the Database for the username with that password:

<?php


session_start();

$host="localhost";

$dbuser="root";

$dbpassword="";

$db="mydb";

$conn = mysqli_connect($host, $dbuser,$dbpassword);

mysqli_select_db($conn, $db);

if(isset($_POST["login"])){


$username = $_POST["user"];
$password = $_POST["pass"];

$sql = mysqli_query($conn, "SELECT username, password FROM users WHERE username ='$username'AND password='$password' limit 1");
if(mysqli_num_rows($sql)==1){
header("location:DailyPlanner.php");
}
else{
echo '<script>alert("Wrong Login Details. Try Again!")</script>';
}

}

?>

And the specific line which is failing me on my settings form:

$_POST['user'] = $username;

$_
POST['pass'] = $password;

i have tried echoing it and switching the post the other way round.

The underscores are shown here but when saving changes it changes with no underscore but all POSTS are $_POST

Thanks for fast reply
Copy linkTweet thisAlerts:
@ginerjmOct 25.2018 — YOUR "the specific line that which is failing" IS NOT SHOWING VALID CODE.

And - please use code tags for all code that you post.

And - use a proper input tag to submit your form. Why are you using a button tag?
Copy linkTweet thisAlerts:
@ASHR48authorOct 25.2018 — @ginerjm#1597118

<?php

session_start();

$host="localhost";

$dbuser="root";

$dbpassword="";

$db="mydb";

$conn = mysqli_connect($host,$dbuser,$dbpassword);

mysqli_select_db($conn, $db);

$_POST['user'] = $username;

$_
POST['pass'] = $password;

?>
Copy linkTweet thisAlerts:
@ASHR48authorOct 25.2018 — @ginerjm#1597118

<?php

session_start();

$host="localhost";

$dbuser="root";

$dbpassword="";

$db="mydb";

$conn = mysqli_connect($host,$dbuser,$dbpassword);

mysqli_select_db($conn, $db);

//'username' & 'password' are the undefined variable I've swapped them around and have no clue what i'm doing as i'm new to $_POST so can you explain what to do please

$_
POST['user'] = $username;

$_POST['pass'] = $password;

?>
Copy linkTweet thisAlerts:
@ASHR48authorOct 25.2018 — The underscore is still not showing
Copy linkTweet thisAlerts:
@ginerjmOct 25.2018 — What underscore? Where did you get this? Have you tried re-typing the code to see if you copied it from somewhere that had a different character set? Why are you attempting to place values INTO the post array?????
Copy linkTweet thisAlerts:
@ASHR48authorOct 25.2018 — @ginerjm#1597123

The underscore for the POST array. I have no clue what i'm doing anymore. All i'm trying to do is get the data the user entered in the input tag called 'user'(on the Login form) which i saved in the $_POST array and i'm trying to transfer this to the settings php file. I researched ways to do this and i saw $_POST and $_GET but i have just had issues the whole time
Copy linkTweet thisAlerts:
@ginerjmOct 25.2018 — And even in your text the underscore is not showing up
Copy linkTweet thisAlerts:
@NogDogOct 26.2018 — Wrap your code here in [ code ] . . . [ /code ] tags (without the spaces I typed here). Otherwise an underscore will be treated as the "markdown" indicator that you want to start or end _italics_ text.
Copy linkTweet thisAlerts:
@rootOct 26.2018 — Underscore the SHIFT + the MINUS KEY results in an underscore.

This thing _

If you are still stuck, bookmark this post and you can copy and paste as you please.
Copy linkTweet thisAlerts:
@NogDogOct 26.2018 — No, it's not a case of the OP not typing the underscore, but the case that it is a special markdown character, so is not being displayed in cases where the forum thinks it's indicating the start or end of _italicized_ text. (I just typed an underscore on either end of "italicized".) If the OP will use ... tags around code blocks or the single "back-tick" quotes around code words within normal text, like this -- $_POST -- then the underscores will be visible in all cases.

E.g., here's what I typed, above (wrapping the "code" in triple-back-tick code block delimiters):
``<i>
</i>No, it's not a case of the OP not typing the underscore, but the case that it is
a special markdown character, so is not being displayed in cases where the
forum thinks it's indicating the start or end of _italicized_ text. (I just typed an
underscore on either end of "italicized".) If the OP will use
...
tags around code blocks or the single "back-tick" quotes around code words
within normal text, like this --
$_POST -- then the underscores will be visible
in all cases.<i>
</i>
``
Copy linkTweet thisAlerts:
@ASHR48authorOct 26.2018 — I'm trying to find the users username and password(that they used to login) by trying to transfer a variable i saved in a post array
<i>
</i>if(isset($_POST["login"])){
<br/>
<i> </i> $username = $_POST["user"];
<i> </i> $password = $_POST["pass"];
<i> </i>
<i> </i>$sql = mysqli_query($conn, "SELECT username, password FROM users WHERE username ='$username'AND password='$password' limit 1");
<i> </i>if(mysqli_num_rows($sql)==1){
<i> </i> header("location:DailyPlanner.php");
<i> </i>}
<i> </i>else{
<i> </i> echo '&lt;script&gt;alert("Wrong Login Details. Try Again!")&lt;/script&gt;';
<i> </i>}
}

This is the login form and i'm trying to transfer the $username and $password variable to my settings file. This is so i can search for all the users data in the database with this username and password then output it. I researched how to transfer a variable and there was various different methods and i tried this one $_POST['user'] = $username;
$_POST['pass'] = $password;

When i run this script and or echo it etc it just says the username and password are undefined variables.
Copy linkTweet thisAlerts:
@rootOct 27.2018 — by trying to transfer a variable i saved in a post array excuse me? Using POST to transfer information from one page to another?? Look at SESSIONS for that.
Copy linkTweet thisAlerts:
@ASHR48authorOct 27.2018 — @root#1597182

i've tried session and GET, i searched and found them three methods and none work
Copy linkTweet thisAlerts:
@ginerjmOct 27.2018 — If you want to pass values from one script to another you use $_SESSION. Show us your code if you still need help
Copy linkTweet thisAlerts:
@ASHR48authorOct 27.2018 — @ginerjm#1597187

I'll try it and if i have any issues i'll let you knwo thanks for the help
Copy linkTweet thisAlerts:
@ASHR48authorOct 28.2018 — I've managed to use the GET method to work by (Login Page) if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$url = "Dailyplanner.php?username=" . $username . "password=" . $password ;

The page i wanted the variables on $username = $_GET['username'];
$password = $_GET['password'];
$url = "DailyPlanner.php?username=" . $username. "password=" . $password;
echo "$username $password";

now this works fine apart from two things. Number 1:

When loading the page the values of $username and $password are shown correctly but some reason it says that password is undefined variable on line 3.

Number 2:

I want to access this variable for multiple pages so i copied it over to the pages i wanted and used the following code
$username = $_GET['username'];
$password = $_GET['password'];
$url = "(the page i want).php?username=" . $username. "password=" . $password;
echo "$username $password";

The errors are just saying that username and password are undefined and it doesn't change the url. Just wondering if anyone can help. Again sorry if this is painful as i am learning PHP. Thanks.
×

Success!

Help @ASHR48 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...