Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Passing Arrays from one form to another using sessions

Hii,

I would like to know how to pass an array from one form to another form using sessions in PHP.

I have used the following but unable to retrieve it

form 1:

$qarray=serialize($qarray1);

session_register("qarray");


Form 2:

$qarr1 = unserialize($_SESSION['qarray']);

Iam getting this error

Notice: unserialize() [function.unserialize]: Error at offset 0 of 92 bytes in C:\Program Files\Apache Group\Apache2\htdocs\quizmaster\post_results.php on line 8

Please help me
Regards
Arunneo

0
Arun_Neo
Asked:
Arun_Neo
1 Solution
 
Big_Red_DogCommented:
What version of PHP are you using?

try this:

form 1

session_start();

// Actually, just use $_SESSION['qarray'][] instead of $qarray1
$_SESSION['qarray'] = $qarray1;



form2

// Instead of using $qarr1, just directly use $_SESSION['qarray']
$qarr1 = $_SESSION['qarray'];
0
 
JonzaCommented:
yes i cant see any problem why it shouldn't work...
and BTW you don't need to serialize that array, sesseion variables can handle arrays too.
0
 
carchitectCommented:
your answer

passarray.php
----------------------
<?php
session_start();
$test[0]="kapil";
$test[1]="dhawan";
$test[2]="good";
$test[3]="boy";
session_register('test');
?>
<html>
<body>
</body>
<a href='passedarray.php'>next</a>
</html>


passedarray.php
----------------------
<?php
session_start();
print_r($_SESSION['test']);
session_destroy();
?>

regards
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Big_Red_DogCommented:
carchitect, you should not promote the old PHP.  It will be deprecated soon and everyone will have to Arun_Neo, stick with $_SESSION[]...
0
 
Big_Red_DogCommented:
In other words, don't use session_register()!!!
0
 
carchitectCommented:
hey hey can you pls make it more clear...
what should i use instead of session _register();
regards
0
 
Big_Red_DogCommented:
You do not need to register your globals if you use $_SESSION[].  Rather than rewriting all of the information, see this page: http://www.php.net/manual/en/function.session-register.php and make sure to read all of the "CAUTION" notes.

There is one statement saying that if register_globals are disabled, session_register() will not work.  It does not explain what "not work" means, but it could mean anything from it has no effect to it will break your application...

So following my original post, that way I've been doing it for quite awhile as I have converted everything to the newer API, is the way you should go.

Here is carchitect's solution corrected:

passarray.php
----------------------
<?php
session_start();
$_SESSION['test'][0]="kapil";
$_SESSION['test'][1]="dhawan";
$_SESSION['test'][2]="good";
$_SESSION['test'][3]="boy";
?>
<html>
<body>
</body>
<a href='passedarray.php'>next</a>
</html>


passedarray.php
----------------------
<?php
session_start();
print_r($_SESSION['test']);
session_destroy();
?>

0
 
carchitectCommented:
thx for correcting me Big Red Dog
regards
0
 
LinkinCommented:
why dont u just use the "Require" funtion, to make all variables and function available in any other php file? =)
0
 
gonzoramaCommented:
Another way to pass arrays between fucntions is to name the html element the same with the [] operators; PHP will automatically allocate an array object. This does not use sessions.

- form.html
<html>
<body>
<form action="process.php" method="post">
<input type="text" name="aData[]" value="one">
<input type="text" name="aData[]" value="two">
<input type="text" name="aData[]" value="three">
<input type="text" name="aData[]" value="four">

<input type="submit" value="GO">
</form>
</body>
</html>

- process.php
<html>
<body>
<pre>
<?
  print_r($_POST['aData']);
?>
</pre>
</body>
</html>

-- the output
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)


HTH

0
 
Big_Red_DogCommented:
Did any of this help you?  If so, please award a grade to close this question.
0
 
Big_Red_DogCommented:
Arun_Neo,

Are you still monitoring this question.

If the information provided was helpful, please award a grade to close the question.

Thank you!
BRD
0
 
ThGCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

> Accept comment from Big_Red_Dog

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

ThG
EE Cleanup Volunteer
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now