I want to pass one session variable to other page. On my first page I have:
<?php
session_start();
?>
then:
<form class="form1" method="post" action="contact2.php" id="form1">
<ul>
<li>
<label for="name">*Name:</label>
<input type="text" name="name" placeholder="Black Nova"class="required" role="input" aria-required="true"/>
</li>
<li>
<input id="submit" class="submit .transparentButton" value="Next" type="submit" name="submit"/>
</li>
</ul>
<br/>
</form>
<?php
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];
?>
In my contact.php I have session start, but I cannot get sesssion variable.
If, in my first page, I dont give any action, I get the right value in $_SESSION['name'] but if I give an action, the session variable wont change. Why?
If you do not give action, then the form is being submitted into the same page and so
$_POSThas value because of which you can get it assigned to$_SESSION. When you give action ascontact2.phpthe form is submitted into a different page and so$_POSTwill not be available in the page that has the form and so the session will not get any value from it.If you have set action to
contact2.php, you can do asession_start()in that page and move the codeinto that page, and you should be able to echo the session in
contact2.php