?
Solved

Passing Arrays from one form to another using sessions

Posted on 2003-02-19
14
Medium Priority
?
175 Views
Last Modified: 2013-12-12
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
Comment
Question by:Arun_Neo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 7

Accepted Solution

by:
Big_Red_Dog earned 300 total points
ID: 7982707
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
 
LVL 1

Expert Comment

by:Jonza
ID: 7985710
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
 
LVL 6

Expert Comment

by:carchitect
ID: 7986125
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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 7988919
carchitect, you should not promote the old PHP.  It will be deprecated soon and everyone will have to Arun_Neo, stick with $_SESSION[]...
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 7988929
In other words, don't use session_register()!!!
0
 
LVL 6

Expert Comment

by:carchitect
ID: 7997879
hey hey can you pls make it more clear...
what should i use instead of session _register();
regards
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 7998678
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
 
LVL 6

Expert Comment

by:carchitect
ID: 8001893
thx for correcting me Big Red Dog
regards
0
 

Expert Comment

by:Linkin
ID: 8051938
why dont u just use the "Require" funtion, to make all variables and function available in any other php file? =)
0
 

Expert Comment

by:gonzorama
ID: 8069039
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
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8547227
Did any of this help you?  If so, please award a grade to close this question.
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8583991
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
 
LVL 14

Expert Comment

by:ThG
ID: 9705128
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question