1

I have one question. So, I have built my PHP application with two text fields (First Name, Last Name) and one submit button (like "Add Contact"). I don't use MySQL. I use array. I want following: The first time when i click submit button I should see the first name and last name of my contact. Second time when i click submit button I should again see the first conact and the new one. Example:

First Click - I see: John Johnson

Second Click - I see: John Johnson (old contact), Peter Peterson (new contact)

Hers is my code:

   <?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Contact {

    private $lastname;
    private $firstname;

    public function getLastname() {
        return $this->lastname;
    }

    public function setLastname($lastname) {
        $this->lastname = $lastname;
    }

    public function getFirstname() {
        return $this->firstname;
    }

    public function setFirstname($firstname) {
        $this->firstname = $firstname;
    }

}

?>



   <?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Controller {

    private $arr;

    public static function addContact($person) {
        $this->arr[] = $person;
    }

    public function getArr() {
        return $this->arr;
    }

    public function setArr($arr) {
        $this->arr = $arr;
    }

}
?>

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" name="form" method="post">
            <table>
                <tr>
                    <td>First Name:</td>
                    <td><input type="text" name="fname" value=""></td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td><input type="text" name="lname" value=""></td>
                </tr>
                <tr>
                    <td><input type="submit" name="submit" value="Add Person"></td>
                </tr>
            </table>
        </form>

        <?php
        include_once 'Contact.php';
        include_once 'Controller.php';

        $controller = new Controller();



        if (isset($_POST['submit'])) {

            $person = new Contact();
            $person->setFirstname($_POST['fname']);
            $person->setLastname($_POST['lname']);
            $controller->addContact($person);

            print_r($controller->getArr());
        }
        ?>
    </body>
</html>

Thanks

2
  • Could you add your code to the question, please? Commented Oct 3, 2012 at 17:31
  • Options are, you can use a session or cookies. Commented Oct 3, 2012 at 17:34

2 Answers 2

2

You need to start a session and add the array to the $_SESSION array:

http://www.thesitewizard.com/php/sessions.shtml

But be warned that the data will only exist as long as the current session exists.

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

1 Comment

Yes, your last sentence is correct: "...data will only exist as long as the current session exists. I know that. I'm actually a JAVA programmaer and there is something like <useBean clas="..." scope="session" id="..." />. Can you write the code here. Thanks
1

As mentioned earlier you can use sessions as a storage, but it only lasts until session timeouts (defaults to 30 minutes).

<?php
session_start();
 if (!isset($_SESSION['names']) || $_SERVER['REQUEST_METHOD'] == 'GET')
    $_SESSION['names'] = array();
if (!empty($_POST)) {
  $_SESSION['names'][] = $_POST['name'];
} 
?>

<?php foreach($_SESSION['names'] as $name): ?>
  <?php echo $name ?>
<?php endforeach; ?>
<form method="post">
  <input type="text" name="name" />
  <input type="submit" value="Add" />
</form>

10 Comments

Yes, your last sentence is correct: "...As mentioned earlier you can use sessions as a storage, but it only lasts until session timeouts (defaults to 30 minutes)". I know that. I'm actually a JAVA programmaer and there is something like <useBean clas="..." scope="session" id="..." />. Thanks
When i click the submit button the array don't have to "refresh" . Can i solve this proble with instances of array or class. And here in your code: if (!empty($_POST)) can i change it with this: if(isset($_POST['submit'])) ???
To see what name submit button has in $_POST, print it out with print_r($_POST); And then you can use isset instead. What do you mean by array refresh?
When i click to reload page. Maybe if you are JAVA programmer you can understand. There is useBean.
See my edit, maybe this is what you meant. Really try to describe your situation better. The more words you use, the more likely we are to understand you.
|

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.