Jump to content

How do I take each input from a form and convert it into a session variable?


tlavelle

Recommended Posts

I have a huge form with 40 text input fields.  Each text field corresponds to a record in a mysql table and was generated using mysql_fetch_row().  Is there an easy while or for loop i can use to populate the $_Sesssion array for each text input?

Try:

 

<?php

foreach ($_POST as $Variable)
{
    $_SESSION[] = $Variable;
}

?>

 

Then whatever the field name is, would be the variable. Example: <input name="Name"> Would be $Name

 

Although, the only problem I can see is that it would store the submit input in a variable as well.

Using

<?php
$_SESSION = $_POST;
?>

probably isn't the wisest solution. I would use either

<?php
foreach($_POST as $fld => $val)
   $_SESSION[$fld] = $val;
?>

or

<?php
$_SESSION['Posted_values'] = array();
foreach($_POST as $fld => $val)
   $_SESSION['Posted_values'][$fld] = $val;
?>

 

To see what's in the $_SESSION array, you can to:

<?php
echo '<pre>' . print_r($_SESSION,true) . '</pre>';
?>

 

Ken

 

Thanks Ken,

That works!

Can you walk me through the logic?  Can you narrate what is going on here?

 

$_SESSION['Posted_values'] = array();
foreach($_POST as $fld => $val)
   $_SESSION['Posted_values'][$fld] = $val;
   
   echo '<pre>' . print_r($_SESSION,true) . '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.