1

Here is my code,

//I have set formData array to session array here
$_SESSION['form_data'] = $formData = array();           

//here i have set a value to formData array
$formData[0] = 'insert data done';

//I want to accually echo the value of formData[0] - key's value        
$var = $_SESSION['form_data'][$formData[0]];

var_dump($var);

I'm new to php and my requirement here is to build a form validation and send the validation data back to the form. So I use the session to send data to one php file to another. I think that's the only way I can send data from one page to another.

2
  • You can also send the data in the query string and access it from the $_GET array, or send a POST request and access it in $_POST. I think those are more common than using sessions in that way. Commented Sep 22, 2012 at 5:20
  • I found this infomation while having a hard search on google think this will be usefull for someone. This array type call Multidimensional Arrays and have explained here well developerdrive.com/2012/01/… php.net/manual/en/language.types.array.php - Example #6 Commented Sep 22, 2012 at 5:46

2 Answers 2

1
// the code only initialize $_SESSION['form_data'] and $formData with an empty array
// after assignment, they're independent.
$_SESSION['form_data'] = $formData = array(); 
// change the $formData won't affect the $_SESSION['form_data']
$formData[0] = 'insert data done';

The right order is:

$formData = array();
$formData[0] = 'insert data done';
$_SESSION['form_data'] = $formData;

$var = $_SESSION['form_data'][0];
var_dump($var);

But for form validation, you don't need to redirect to the input page if validation failed. Instead, just use the same template (html content), and render the page with error message, this way, you don't need to send data back to the input page.

Sign up to request clarification or add additional context in comments.

Comments

0

use it like this

$formData[0] = 'insert data done';

$_SESSION['form_data'] = $formData;  

$var = $_SESSION['form_data'];

var_dump($var);

now you can use $_SESSION data in any page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.