PDA

View Full Version : PHP post and session question


leaded
4th January 2005, 09:48 PM
I have 3 php pages. The first two are forms and the third redisplays the form fields' values.

I want to use $_SESSION to store all of my form values because I'll be working on 3 or 4 pages eventually and I want to be able to call them up easily. I have a previous version of what I'm trying to do but I have to session_register EVERY value. All I knew at the time was some ASP and I used a ASP to PHP converter and it turned it into crap. Now I'm trying to make a clean and polished PHP script but I'm having a little bit of trouble.

Basically I'm trying to add the values of $_POST to the existing $_SESSION. It doesn't work tho... here's my code...

test1.php<html>
<body>

<form method="post" action="test2.php">
Church Name: <input type="text" name="name" size="50"><br>
Church Phone: <input type="text" name="phone" size="50"><br>
<input type="submit">
</form>

</body>
</html>

test2.php<?php
session_start();

$_SESSION = $_POST;
?>

<html>
<body>

<form method="post" action="test3.php">
Contact Name: <input type="text" name="contact_name" size="50"><br>
Contact Phone: <input type="text" name="contact_phone" size="50"><br>
<input type="submit">
</form>

Testing form 1's values...<br>
<?php
echo $_SESSION['name'];
echo $_SESSION['phone'];
?>

</body>
</html>

test3.php<?php
session_start();

array_push($_SESSION, $_POST);
?>

<html>
<body>

Name: <?php echo $_SESSION['name']; ?><br>
Phone: <?php echo $_SESSION['phone']; ?><br><br>

Contact name: <?php echo $_SESSION['contact_name']; ?><br>
Contact phone: <?php echo $_SESSION['contact_phone']; ?><br>

</body>
</html>

This only displays $_SESSION['name'] and $_SESSION['phone']. I also tried $_SESSION .= $_POST; But that just outputted "A" for each value for some reason.

I'm not a PHP pro, I'm teaching myself, and I'm stuck here. Once I can get this concept working I can build up the rest of my forms.

Thanks in advance!

joe_0
4th January 2005, 10:00 PM
Hi,

You may find it better/easier to just stick 'name' and 'phone' in hidden fields on test2.php. But anyway, the problem with 'array_push' is that it is just sticking $_POST into $_SESSION as a single array - if you do 'print_r($_SESSION)' at the end of test3.php, you may see what I mean. If you want to add the post variables to the session, then you could use this code:

foreach($_POST as $key=>$value){
$_SESSION[$key]=$value;
}

There may be a better way of doing this, but it should work. Another solution, if you know what fields to be expecting, would be to just use:

$_SESSION['contact_name']=$_POST['contact_name'];
//etc...

But I guess that is a bit harder to update later.

Anyway, hope that helps a bit.

Regards, Joe.

EDIT:
Or, you could use 'array_merge()': :)
http://uk2.php.net/manual/en/function.array-merge.php

So:

$_SESSION=array_merge($_SESSION,$_POST);


PPS, by printing the post variables from test1.php into the form on test2.php, you will be giving support to users who aren't using cookies - though probably not that many! So use:

foreach($_POST as $key=>$value){
echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" />\n";
}

on test2.php, and then you can grap it all out of $_POST at the end. Up to you.

Feel free to PM me if you want any more PHP help - I suppose this isn't really fedora related. :) I'm no expert when it comes to Fedora, but happy with PHP. :cool:

leaded
5th January 2005, 12:36 AM

Thanks a lot!!!

The array merge works like a charm, and is exactly what I needed!

Although, I really like the last foreach loop you wrote... I might use that if I run into cookie issues.

Thanks again!

leaded
5th January 2005, 12:48 AM
Although... \n doesn't seem to be doing anything on this server...?

<?php
echo "Hello";
echo "\n";
echo "Hello";
?>

Simply outputs "Hello Hello"...

Is there something wrong here?