0

I want to add form data into an array using session.
How can I do this please help me.
My HTML form is:

    <form action="#" method="post">
    Roll No:<input type="text" name="rollno" value="" id="rollno"  />
    <input type="submit" name="submit" value="Submit" />
    </form>

And my PHP code is:

    <?php
      session_start();
      $_SESSION['rollno'] = isset($_POST['rollno']);
      echo $_SESSION['rollno'];              
    ?>

I want to insert roll no into an array.

I have a record of 10 students. When I am inserting the first student Roll No then it prints the roll no, but when I insert the second student Roll No it overwrites the first student record.

I want to display all 10 student roll nos on same page.
How can I do this?

6
  • Then just append to the array. Commented Mar 15, 2013 at 11:56
  • 2
    Make the session var an array and use 'array_push' to add to it Commented Mar 15, 2013 at 11:56
  • I've never used # as the action such as <form action="#" method="post">. Think it is a good idea? Commented Mar 15, 2013 at 12:00
  • @user1032531 it will point the form to itself Commented Mar 15, 2013 at 12:01
  • @Ander2. Thanks. Figured so. Think it is a good idea? Any security risks? Commented Mar 15, 2013 at 12:02

5 Answers 5

2
  1. Start the session before you out put any thing to the page, ie before html code

  2. Make session a multi dimensional array

  3. Remove isset from isset($_POST['rollno']);

    <?php
          session_start();
          $_SESSION['rollno'][] = $_POST['rollno'];
          print_r($_SESSION['rollno']);              
    ?>
    
Sign up to request clarification or add additional context in comments.

2 Comments

how multi dimensional array will be created?
@AnjaniGupta. PHP will automatically make it. Probably more readable if you define it as one if it is currently not set.
1

$_SESSION['rollno'] should be an array not a simple variable.

Something like this:

  <?php
  //Define somewhere $_SESSION['rollno'] as array. ONLY ONCE. Note that session must be started.
  session_start();
  if (!isset($_SESSION['rollno'])){
      $_SESSION['rollno'] = array();
  }


  if(isset($_POST['rollno'])){
       array_push($_SESSION['rollno'],$_POST['rollno']);
  }
  foreach ($_SESSION['rollno'] as $item){
        echo $item;  
  }   
  ?>

3 Comments

Need session_start() before using sesssions.
I tried this but it print only last inserted value . I have to print all the 10 records.
@AnjaniGupta make sure $_SESSION['rollno'] is being defined only once. Otherwise you are creating the variable in every run, so it will only store the last value. I've edited the example.
0
session_start();
if(isset($_POST['submit'])){
  if(isset($_POST['rollno'])){
      $_SESSION['rollno'] = $_POST['rollno'];
      echo $_SESSION['rollno'];        
  } 
}

Check first if form is submitted, then roll is set and if yes, assign it to session var.

Comments

0

i know !

Let's try :

<?php
session_start();
$_SESSION['rollno'] = Array();
$_SESSION['rollno'][] = $_POST['rollno']; 
$_SESSION['rollno'][] = $_POST['rollno'];
var_dump($_SESSION['rollno']);
?>

You can access it from loop too .. Good luck

Comments

0

Almost there

<?php
  session_start();
  !isset($_SESSION['rollno']){$_SESSION['rollno']=array();}
  $_SESSION['rollno'][] = $_POST['rollno'];
?>

Also, recommend setting action to something:

echo("<form action={$_SERVER['PHP_SELF']} method='post'>");

Some don't like to use PHP_SELF and recommend hard coding it

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.