0

I want to do the following without JavaScript, AJAX or a database. Only HTML and PHP.

Here's the scenario:

I have two HTML inputs in a form. This form sends the two values (name and age) by POST method. I create a user object from these values, store this values in a SESSION and then prints the users' information below the form. I have no problem creating the user object.

How can I do it in such a way that it prints out all users' information. For example first users enter his information and clicks submit, we have:

 - John
 - 18 years

Second user enter his information and clicks submit, we have:

- John
- 18 years

- Kevin
- 20 years

Third user enter his information and clicks submit, we have:

- John
- 18 years

- Kevin
- 20 years

- Bryan
- 19 years

and so on?

Thanks!

3
  • 3
    Save them in a CSV, XML or JSON file on server's local disk file system? (oh well, you're reinventing a database here.. why not just using it?) Please note that HTTP sessions are not shared across users (that would be a huge security hole) Commented Jan 31, 2013 at 19:26
  • @BalusC yeah i understand the security reasons but this isn't a website that would be used by other people. I know that it would be better to create a database but my aim here is totally different. I'm just practicing and I want to do this on one PHP file. Commented Jan 31, 2013 at 19:33
  • @BalusC +1 for pointing to security hole when sessions are shared between users Commented Jan 31, 2013 at 19:33

3 Answers 3

2

You should store the data in an array, and append to the array on each submit.

<?php

    session_start();
    $data = array();
    if(isset($_SESSION['data'])) {
        $data = $_SESSION['data'];
    }
    if(isset($_POST['name']) && isset($_POST['age'])) {
        // -- append to the array
        $data[] = array('name'=>$_POST['name'],'age'=>$_POST['age'],);

        // -- update the session
        $_SESSION['data'] = $data;
    }
?>
<!DOCTYPE html>
<html>
 <head>
 </head>
 <body>
    <form action="" method="post">
      Name:<br />
      <input type="text" name="name" size="10" /><br />
      Age:<br />
      <input type="text" name="age" size="2" /><br />
      <br />
        <input type="submit" />
    </form>
    <?php foreach($data as $d) { ?>
        - <?php echo $d['name']; ?><br />
        - <?php echo $d['age']; ?><br /><br />
    <?php } ?>
 </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the illustration =)
2

you can keep storing these things in sessions, for later use in the same session. but another approach could be to store them in cookies, both ways will only work if you enter all information on 1 computer.

if you want to make your site output this to all other users, you need an database or something else like that

1 Comment

Yeah that's what I want to do but storing them in session on each submit overwrites the session's content. I don't want - shouldn't - to use cookies. Let's say its kind of a homework and yes I enter all information on 1 computer.
1

Say, your code is something like

$name = $_Post['name'];
$age = $_Post['age'];

and then

use array of sessiona variable for $name and $age

also, please see this astonishing answer by Kaleb Brasee , on the same link, Sarfraz has explained it in bit of details..will help you a lot

1 Comment

Astonishing indeed. Thanks a lot for the link !

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.