0

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?

4
  • What does echo $_SESSION['name'] produce before you set it to $_POST['name']? Commented Aug 22, 2012 at 15:16
  • 2
    Is your call to session_start() on the contact page before any output to the browser? Show us where it is called. Commented Aug 22, 2012 at 15:17
  • Can you post both files with the right names, it's a bit unclear what the first page, contact.php and contact2.php files are and contain. Commented Aug 22, 2012 at 15:21
  • Michael Berkowski-sesstion start is called in very first line of the page. Commented Aug 22, 2012 at 15:22

3 Answers 3

1

If you do not give action, then the form is being submitted into the same page and so $_POST has value because of which you can get it assigned to $_SESSION. When you give action as contact2.php the form is submitted into a different page and so $_POST will 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 a session_start() in that page and move the code

<?php
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];
?>

into that page, and you should be able to echo the session in contact2.php

Sign up to request clarification or add additional context in comments.

2 Comments

What do I do to get data from the form to the next page?
You can assign them to variables..all the form data will be available in the super global called $_POST. For e.g to get the value in the textbox called name you can get it as $name = $_POST['name'].. you can do the same for any/all other fields you have in the form
1

Its because if you don't supply any action, the form will submit to itself (same page) and because your setting the session variable in the first page it fills the session variable fine. When you add the action to the 2nd page it never fills the session variable because the first page isn't receiving the $_POST, the 2nd page is.

if your submitting form data to a different page, you need to set the session data on the receiving page not the sending page.

1 Comment

Mother Goose the reason your having an issue is your trying to set session variables on the first page that relates to form data that hasnt been submitted yet. Simply move "<?php $_SESSION['name'] = $_POST['name']; echo $_SESSION['name']; ?>" to contact2.php and all will work. remember to start the session on contact2.php too. This is presuming your form is on a different php page e.g contact.php
0

When you click submit in your form, form will send you in contact2.php (action="contact2.php"). Create contact2.php and write code below:

  session_start();

  session_regenerate_id();

  $_SESSION['name'] = $_POST['name'];

  echo $_SESSION['name'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.