1

一つ質問があります。そのため、2つのテキストフィールド(名、姓)と1つの送信ボタン([連絡先の追加]など)を使用してPHPアプリケーションを作成しました。MySQLは使用していません。配列を使用しています。次のようにしたい:初めて送信ボタンをクリックすると、連絡先の名前と名前が表示されます。2回目に送信ボタンをクリックすると、最初の契約と新しい契約が再び表示されます。例:

ファーストクリック-なるほど:ジョン・ジョンソン

セカンドクリック-なるほど:ジョン・ジョンソン(古い連絡先)、ピーター・ピーターソン(新しい連絡先)

彼女は私のコードです:

   <?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>

ありがとう

4

2 に答える 2

2

セッションを開始し、配列を $_SESSION 配列に追加する必要があります。

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

ただし、データは現在のセッションが存在する限り存在することに注意してください。

于 2012-10-03T17:32:30.947 に答える
1

前述のように、セッションをストレージとして使用できますが、セッションがタイムアウトするまでしか持続しません (デフォルトでは 30 分)。

<?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>
于 2012-10-03T17:41:14.753 に答える