At the top I have a session start and the session ID can be passed across all the webpages because the ID is the same on all, this also includes a session variable called 'username' with the value of Guest.
I want to change this variable to the one entered in the form.
This is my first post so sorry for any mistakes.
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
Thanks
You need to take out ./ is in the action="" quotes like below (this is because you are using the same file to process the form)... and always start your php opening with <?php
<form class="form1" method="post" action="" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
And if you want to test it try something like this:
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
Tested the code after making the changes, and it works fine... look:
Update answer based of requests in the comments
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
PUT THIS IN YOUR index.php
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
Related
I have a page with a form that needs to be sent via post.
My htaccess has redirects in it causing the post data to be lost so I would like to put that data into session variables to be picked up by another page.
The session variable "favcolor" below on the form page is there just as a tester.
The output on the second page should show the data from the form but it is not. It's an empty array. It is showing the color variable ok though.
I've cut the code right back here to the simplest few lines to test this out and can't get it to work. Can anyone help point me in the right direction here please?
Form page:
<?php
session_start();
$_SESSION['post-data'] = $_POST;
?>
<form action="zv.php" method="post">
Name:<br>
<input type="text" id="inputName" name="inputName">
<br>
Email:<br>
<input type="text" id="inputEmail" name="inputEmail">
<br>
Telephone Number:<br>
<input type="text" id="inputTel" name="inputTel">
<input type="submit" id="submit" value="Submit">
</form>
<?php
$_SESSION["favcolor"] = "green";
?>
Second page (zv.php):
<?php
session_start();
print_r($_SESSION['post-data']);
echo "<br>";
print_r($_SESSION);
?>
Output from second page (zv.php):
Array ( )
Array ( [post_data] => Array ( ) [post-data] => Array ( ) [favcolor] => green )
You need to set this ($_SESSION['post-data'] = $_POST) only if you have post request.
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['post-data'] = $_POST;
header('location: zv.php');
}
?>
<form action="<?=$_SEVER['PHP_SELF']?>" method="post">
Name:<br>
<input type="text" id="inputName" name="inputName">
<br>
Email:<br>
<input type="text" id="inputEmail" name="inputEmail">
<br>
Telephone Number:<br>
<input type="text" id="inputTel" name="inputTel">
<input type="submit" id="submit" value="Submit">
</form>
Then you can access the Session data in the second page.
<?php
if (isset($_POST['inputName'])) {
session_start();
$_SESSION['post-data'] = $_POST;
header('location: zv.php');
}
?>
<form method="post" action="<?php echo $_SEVER['PHP_SELF']; ?>" >
Name:<br>
<input type="text" id="inputName" name="inputName">
<br>
Email:<br>
<input type="text" id="inputEmail" name="inputEmail">
<br>
Telephone Number:<br>
<input type="text" id="inputTel" name="inputTel">
<input type="submit" id="submit" value="Submit">
</form>
My point is after sending form show session data.
form.html:
<form action="sendForm.php" name="f" method="post" onsubmit="return sendForm()" >
<div class="form">
<label for="imie">Name</label>
<input type="text" id="nam" name="nam"/><br/>
<label for="nazwisko">Surname</label>
<input type="text" id="surname" name="surname"/><br/>
</div>
<input type="submit" name="send" id="send" value="send form"/><br/><br/>
</form>
sendForm.php:
<?php
session_start();
$_SESSION['nam'] = $_POST['nam'];
$_SESSION['surname'] = $_POST['surname'];
header("Location: view-struct.php");
?>
view-struct.php:
<span class="label">Name</span><?php echo $_SESSION['nam']; ?><br/>
<span class="label">Surname</span><?php echo $_SESSION['surname']; ?><br/>
[EDIT] Question is closed, forgot to add for session_start() in view-struct.php
Seems, that I can't add password protection to the script: it should allow to login with the pass and to submit data from the form to mysql. Login looks fine, but if I try to press submit, it returns me to login page. Seems, that session is dropped or overwritten, but is not clear, how:
//login area
<?php
$password = "test";
session_start();
$_SESSION['txtPassword']= $_POST['txtPassword'] ;
if ( $_SESSION['txtPassword']!=$password ) {
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><label for="txtPassword">Password:</label>
<br /><input type="text" title="Enter your password" name="txtPassword" /></p>
<p><input type="submit" name="Submit" value="Login" /></p>
</form>
<?
}
elseif ( $_SESSION['txtPassword']=$password ) {
echo $_SESSION['txtPassword'] ; // tried to print password, result is correct: test
//my db connection, just in case:
include "config.php";
$connect = mysqli_connect(HOST, USER, PASSWORD, NAME);
// data which should be inserted to db
if
(#$_POST['posted']=='1' $_POST['posted'])) {
$sSQL = "UPDATE users SET user_login='".mysqli_real_escape_string($connect, $_POST['usern'])."',user_pass='".mysqli_real_escape_string($connect, dohashpw($_POST['passw']))."' WHERE ID=1";
mysqli_query($connect, $sSQL) or print(mysql_error());
print ' <div class="container"> <p class="pstype">Password updated! </p>';
...
//input form
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="posted" value="1" />
<div class="col-xs-3">
<label for="ex2">New Username: </label>
<input type="text" class="form-control input-lg" name="usern" >
</div>
<div class="col-xs-3">
<label for="ex2">New Password: </label>
<input type="password" class="form-control input-lg" name="passw" >
</div>
<div class="col-xs-3">
<input type="submit" value="Submit" onclick="<? mysqli_query ($connect, $sSQL);?>; ">
</div>
</form>
I am able to login this page, but when I fill the form and click Submit, I get login area again. If echo $_SESSION show a correct result, I think that it was established, but data are lost after for submit. Could you please help to find my error?
You are assigning and not comparing here :
elseif ( $_SESSION['txtPassword']=$password ) {
this is better
elseif ( $_SESSION['txtPassword']==$password ) {
but thats a bad idea anyway, passwords should not be stored in session variables like this, and you have to hash them once the user submit them and only manipulate and store the hashed passwords in your code and database
<?php
$password = "test";
session_start();
$_SESSION['txtPassword']= $_POST['txtPassword'] ;
if($_SESSION['txtPassword']!=$password ){
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ? >">
<p><label for="txtPassword">Password:</label></p>
</br>
<p><input type="text" title="Enter your password" name="txtPassword"/> </p>
<p><input type="submit" name="Submit" value="Login"/></p>
</form>
<?php
}
else{
echo $_SESSION['txtPassword'];
}
?>
i am not understanding why the elseif stands for? you are already checking inside the if condition which both are not equal?.
How do I hide the authorization form when the user is signed in? Instead, I want the user to see: Welcome, %username%.
My code so far
<?if(!isset($_SESSION['id'])):?>
<form action="testreg.php" method="post" }>
<p>
<label><span">Username:</span></span><br>
</label>
<input name="login" type="text" size="25" maxlength="15">
</p>
<p>
<label><span class="style3">Password:</span><br>
</labe l>
<input name="password" type="password" class="style3" size="13"
maxlength="15">
</p>
<p><input name="submit" type="submit" value="Enter">
Need an account?</</p>
</form>
<?php
if (empty($_SESSION['login']) or empty($_SESSION['id']))
{
echo "You have signed in<br><a href='#'>Available only for you</a>";
}
else
{
echo "Welcome, ".$_SESSION['login']."<br><a href='link'>Profile</a>";
}
?>
<?endif?>
give your form an ID like this
<form id="loginForm" action="testreg.php" method="post">
and in your PHP where you welcome the user, throw in some inline-style.
if (empty($_SESSION['login']) or empty($_SESSION['id']))
{
echo "You have signed in<br><a href='#'>Available only for you</a>";
}
else
{
echo ' //Add this bit here
<style>
#loginForm{
display:none!important;
}
</style>
';
echo "Welcome, ".$_SESSION['login']."<br><a href='link'>Profile</a>";
}
Using this way you don't need to go on other page for signin
<?php ob_start(); ?>
<form action="testreg.php" method="post" }>
<p>
<label><span">Username:</span></span><br>
</label>
<input name="login" type="text" size="25" maxlength="15">
</p>
<p>
<label><span class="style3">Password:</span><br>
</labe l>
<input name="password" type="password" class="style3" size="13"
maxlength="15">
</p>
<p><input name="submit" type="submit" value="Enter">
Need an account?</</p>
</form>
<?php $form = ob_get_clean(); ?>
//logic goes here
if (empty($_SESSION['login']) or empty($_SESSION['id']))
{
echo $form;
}
else
{
echo "Welcome, ".$_SESSION['login']."<br><a href='link'>Profile</a>";
}
haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...