jimmynew
jimmynew

Reputation: 9

$POST data in $SESSIONS

Why it's not working?

index.php:

<strong>Your name:</strong>
<form action="name.php" method"post">
  <input type="text" name="username"/>
  <input type="submit" name="Submit" value="Submit!" />
</form>
<?php 
  session_start();
  $_SESSION['post-data'] = $_POST;
?>

name.php:

<strong>Your name is:</strong>
<strong>
  <?php 
    session_start();
    echo $_SESSION['post-data'];
  ?>
</strong>

name.php print me:

Your name is: Array

How can I fix this?

Upvotes: 0

Views: 2230

Answers (1)

Kitson88
Kitson88

Reputation: 2940

Firstly, session_start(); should always be placed at the top of your document before any HTML has been loaded.

index.php

//No need to start session here as no $_SESSION variables needed. 

<strong>Your name:</strong>
<form action="name.php" method="post">
<input type="text" name="username"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

Secondly, assign your $_POST data after it's been posted to name.php and then assign it to a $_SESSION

name.php

<?php
session_start();
$_SESSION['username'] = $_POST['username'];
?>

<strong>Your name is:</strong>
<strong><?php echo $_SESSION['username'];?></strong>

If you want see whats inside an array for debug purposes then you can use var_dump().

Example of var_dump()

var_dump($_POST);

or

var_dump($_SESSION);

Upvotes: 1

Related Questions