Jump to content

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


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>';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.