Session variables from form not showing on next page - php

I can set session variables and use them on another page. However when i try to use a simple contactform with a username and email address and try to store them into session variables, they don't show up on other pages. There must be something basic i'm missing.
Here's the form:
<?php
session_start();
$submit = $_POST["submit"];
if($submit){setSessionVars();}
function setSessionVars() {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
header('Location: session.php');
}
?>
<html>
<body>
<form action="session.php" method"post">
<input name="name" type="text" value="Name" size="11" /><br />
<input name="email" type="text" value="Email" size="11" /><br /><br />
<input name="submit" type="submit" value="Submit" size="11" />
</form>
</body>
</html>
And this is session.php:
<?php
session_start();
echo $_SESSION['name'];
echo $_POST['name'];
?>
Also
header('Location: session.php');
is not working. Any ideas?

At a glance, I see one immediate problem that will keep the form from posting.
<form action="session.php" method"post">
You need an "=" sign between method and "post".
Changing that alone will give you the "t" in session.php.

You post the form to session.php:
<form action="session.php" method"post">
I'd change it to:
<form method="post">
That way, the page posts to itself. Then it can register the session variables and redirect the user to session.php.
Edit: also, you forgot the = sign in method"post".

Related

Storing form data in session with php

I'm testing a really basic PHP form, where the form data is saved in a session.
Later, i want that session data to be the default value of the form:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="var" value=<?php $name ?>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
}
echo $name;
?>
So, for example if i input "MyName" it should echo "MyName" and in the form there should be the value "MyName". The problem with the actual code is that it gives an E_NOTICE : type 8 -- Undefined variable: name -- at line 18 error. I think that the variable is not being stored, can someone help me out on this?
The first error I notice is this piece of code:
<form action="" method"post">
Where method does not contain the symbol "=" which causes the loss of the parameter post.
Furthermore, the "session_start ()" function must be placed before any other code. The code derives from this is as follows:
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
} else {
$name = null;
}
?>
<strong>Test Form</strong>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="var" value="<?= ($name != null) ? $name : ''; ?>">
<input type="submit" name="Submit" value="Submit!" />
</form>
Spotted a few things:
I'd put the session_start(); at the top of the page before outputting anything.
Your method was incorrectly written it needs a '=' when specifying the method - thats the main reason why nothing was being stored, the form wasn't submitting properly.
Same with how you've put in the value on the name input - it has no '=' and you don't close the input tag properly - I've left it blank and added a placeholder - you can change it to what you need.
Heres how I'd do it:
<?php session_start(); ?>
<strong>Test Form</strong>
<form action="" method="post">
<input type="text" name="var" value="" placeholder="enter name">
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
}
// Store the session in a variable after the submit - otherwise it will be forgotten on refresh
$name = $_SESSION['var'];
// check if session exists
if(isset($name)) {
echo $name;
}
else {
echo 'no name entered...';
}
?>
You can edit the above to hide the form if a name has been submitted etc. Use session_destroy(); to reset the stored session.
Cheers

Conflict On Two Post on Single Page Form(HTML) and PHP

I need to keep two forms for login and Logout in one single page including PHP and HTML like
<html>
<body>
<form action="" method="post">
<input type="text" name="username">
<input type="submit" name="SubmitButton" value="Get in"/>
</form>
<form action="" method="post">
<input type="submit" name="logoutButton" value="Logout"/>
</form>
</body>
</html>
on PHP part I have
<?php
if(isset($_POST['SubmitButton'])){
$inputuser = $_POST['username'];
$_SESSION['user'] = 'A';
}
if(isset($_POST['logoutButton'])){
unset($_SESSION['user']);
header('Location: http://somewhere.com/');
}
As I said I have to keep everything on one single page but this looks like causing conflict between the POST(s) can you please let me know how to stop this and target each Post properly?

Header location input error

I have problems with custom search input that redirects to id:
mysite/search/$id.
The thing is that is working, but.. when I'm at this site:
http://mysite/search/$id after entering again into input some id it display it as mysite/search/search
How can I solve it?
The code I use
$name = $_POST['name'];
header("Location: search/$name");
HTML:
<form method="post" action="search"> <input type="text" name="name"> <input type="submit"> </form>
Where is $id defined? It looks like you want
header("Location: search/$name");
EDIT:
After seeing your form, you'll want to change your PHP to:
$name = $_POST['name'];
header("Location: search/" . $name );
EDIT:
I have tested this and it works. Your action in the form is what's causing the issue. Change your code to this
<?php
if(isset($_POST['name'])){
$name = $_POST['name'];
header("Location: /search/$name");
}
?>
<form method="post"> <input type="text" name="name"> <input type="submit"> </form>

PHP: Redirect same page after click the submit button

Is there a way to redirects to the same page using PHP.??? I just need to reload or refresh the gridview. I don't need to redirect to a website or link, I need is redirect it to the same page or same form like main.php.
I already used the header(), but its not working to me and all I see is linking in the website.
Thanks.
Simply do:
$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
You should redirect with a location header after every post, because otherwise when the user presses the refresh button, it will send again the same form...
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
file_put_contents('data.txt', $_POST['data']);
header('location: ' . $_SERVER['PHP_SELF']);
} else {
header('content-type: text/html; charset=utf-8');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="application/x-www-form-urlencoded; charset=utf-8">
<input name="data" type="text" value="<?php echo file_get_contents('data.txt'); ?>"/>
<button>küldés</button>
</form>
<?php
}
Btw. if you want to do proper work, you should try out a php framework instead of this kind of spaghetti code...
Here are two example to redirect to a form.
Lets say that your filename is main.php
<form action="main.php">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>
Or you can use this
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>
Did that answer your question?

PHP sessions problems

I'm using sessions to save what ever the user types in the form and what ever they do type will be displayed on the other pages.
It was working perfectly fine but after all the server uploads and such my code has completely done one on me and i'm lost.
Can somebody see if they can spot a mistake? I need fresh eyes.
HTML.
<div id="form"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start(); // declaring the variable
if(isset($_POST['fullName'])){ //setting the variable
if(empty($_POST['fullName'])){ //if empty dont to nothing but my wep page will reload
}else{ //if they have do this
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php"); //then will direct the user to the home page (will also display name on each page)
}}
?>
Session on other pages
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName']
?>
</div> <!--div ECHO end -->
$_SESSION['fullName'] = $_POST['fullName'];
session_register(fullName);
replace with this code try it
You'll need to add session_start() on whatever page you are redirecting to that is supposed to display the data.
Also, (I'm assuming you realize) what you posted doesn't have anything that would output the data, like:
<input type="text" name="fullName" value="<?php echo $_SESSION['fullName']; ?>"/>
You need to start session on other page as well and stop the script from setting that session. After header location you need to use exit here.
<?php session_start();?>
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName'];
?>
you need use exit after header location :-
header('location: home.php');
exit;
Just change the div id form to other because it has a default and remove the empty function because you add isset functon.
Use this.
<div id="myform"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start();
if(isset($_POST['fullName']))
{
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php");
exit();
}
?>
Session on other pages.
<div id="echo"> <!-- div ECHO start -->
<?php
session_start();
print_r($_SESSION);
echo $_SESSION['fullName'];
?>
</div> <!--div ECHO end -->
May be it helpful to you.If any problem then let me know.
You are "posting" the values to home.php, doing that you can't set $_SESSION['fullName'] = $_POST['fullName'] in the origin.
Change
<form action="home.php" method="post">
to
<form action="name_of_the_first_script.php" method="post">
$_POST['fullName'] does not exist before the redirect.
Here is how everything should look like (lest call the page index.php):
<div id="form"><!--Form Start-->
<form action="index.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
now after you hit submit the index.php will be reactioned and at this time with the $_POST request meaning that that the condition
if(isset($_POST['fullName'])){
will be true and the PHP code can be executed, setting the $_SESSION variable and redirecting you to home.php where you ca now read the $_SESSION previously set in index.php
Hope this can me more clear now! :)

Categories