1

I've been searching for this solution for a while now and found some thread that were made previously regarding my same problem. But I still could not solve my problem. It's been days now and I still can't keep the array data from my HTML form to be stored in session. It just get overwritten every single time. Is there something wrong my my coding?

This is my PHP file that processes the input

<?php 
    if(!isset($_SESSION)){
        session_start();
    }

    $student1 = array(
        array(
            'Name'=>$_POST['name'],
            'Matric-No'=>$_POST['matric'],
            'Gender'=>$_POST['gender'],
            'Day'=>$_POST['DOBDay'],
            'Month'=>$_POST['DOBMonth'],
            'Year'=>$_POST['DOBYear'],
            'Citizen'=>$_POST['citizen'],
            'Marital'=>$_POST['kahwin'],
            'Religion'=>$_POST['religion'],
            'Active'=>$_POST['active'],
            'Year-of-Study'=>$_POST['Cyear'],
            'ID-Number'=>$_POST['idno'],
            'Email'=>$_POST['email']
        )
    );

    $_SESSION['data'] = $student1;

    print_r($_SESSION);

?>

*Edit: So sorry... I forgot to mention that I do have a javascript validator to see if the user has successfully entered every form before submitting. And my problem is that when I refresh or go back to the form site, the previous data will not be there and if there is any new data that is entered. The previous data will be overwritten

2
  • I don't quite understand your issue. Are you getting any errors? What do you expect to happen that is not currently happening? Is the $_POST array empty or filled with your expected values? Commented Nov 28, 2015 at 21:09
  • The data is inserted perfectly but the thing is, it wont keep the current data if there is another form that is going to be inserted. I'm trying to make a form that will keep its data in an array so that it can calculate a few things from the collected data such as their citizenship,religion, age and so on. I do have a JavaScript Validator to see if the user input every data in the form Commented Nov 28, 2015 at 21:42

2 Answers 2

2

You must set the $_SESSION['data'] only if a form has been submitted. Here is an example testing if name and religion has been sent (You could test for all variables to be sure):

session_start();

if ( isset( $_POST['name'] && isset( $_POST['religion'] ) {
    $student1=array(
        'Name'=>$_POST['name'],
        'Matric-No'=>$_POST['matric'],
        'Gender'=>$_POST['gender'],
        'Day'=>$_POST['DOBDay'],
        'Month'=>$_POST['DOBMonth'],
        'Year'=>$_POST['DOBYear'],
        'Citizen'=>$_POST['citizen'],
        'Marital'=>$_POST['kahwin'],
        'Religion'=>$_POST['religion'],
        'Active'=>$_POST['active'],
        'Year-of-Study'=>$_POST['Cyear'],
        'ID-Number'=>$_POST['idno'],
        'Email'=>$_POST['email']
    );

    $_SESSION['data']=$student1;
}

print_r($_SESSION);
?>

Now the $_SESSION['data'] is only changed when you POST a form with name and religion!

EDIT: If you want to add more than one student in the session, try something like this:

$_SESSION['data'][]=$student1;

or simply:

$_SESSION['data'][]=$_POST;

To retrieve the data you do something like this:

foreach ( $_SESSION['data'] as $data )
    echo 'Name: ' . $data['Name'];

or

for( $i = 0; $i < count( $_SESSION['data'] ); $i++ )
    echo 'Name: ' . $_SESSION['data'][$i]['Name'];

Edit 2 Removed extra array from $student1 variable

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

9 Comments

I'm so sorry for not include that I have a javascript that validates the form to see if the user has completely enter all the form that are needed. My problem is that when I refresh or go back to the form site, the previous data will not be there and if there is any new data that is entered. The previous data will be overwritten
Are you looking for something like <input type="text" name="name" value="<?= $_SESSION['data']['name'] ?>"> in the form to keep the previously entered values?
Yes! Something like that so that I could maybe have like 3 student information stored in the session array and can obtain them without it being overwritten by itself. The reason for me to obtain the information back is so that I could manage and count the number of female and male students, their average age, citizen and etc
Thanks a lot dude! This solve my problem! But I did not do the foreach and for loop though. When I do that it just shows me the first inserted value and then gave me this error : Notice: Undefined index: Name in C:\xampp\htdocs\studentprocess.php on line 15 name: But none the less, I changed it by using only print_r($_SESSION); and it works wonders. Dude, I owe you a lot man. Thanks! I was stuck with this problem since like 2 days ago and still could not move on until today. Kudos to you good sir! I'm utterly grateful.
I wish I could thumbs you up but my reputation is still 1. So sorry man
|
0

You are missing session_start() to start session itself.

And make queries such $_SESSION['data'] = [your_array]; and test as isset($_SESSION['data'])

Check this page http://php.net/manual/en/ref.session.php

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.