1

変数を変数に格納する際に問題があり$_SESSIONます。Zend フレームワークを使用して、3 ステップのアプリケーション フォームを作成しています。最初のステップが完了したら、データを MySQL データベースに保存し、返された挿入 ID をセッション変数に保存します。次に、ページを別のコントローラーに転送します (ステップ 2)。リクエストを転送すると、すべてが正常に機能し、セッション変数から ID を読み取ることができます。しかし、2 番目のフォーム (ステップ 2 と同じコントローラーをアクションとして持つ) を送信すると、セッションが失われます。試してみるとvar_dump、 が返されますNULL

コードは次のとおりです。

public function organizationAction()
{

    $this->view->vals="";
    $form=$this->getOrganizationForm();
    $this->aplid=$_SESSION['appid'];
    var_dump($_SESSION);
    $firsttime=$this->getRequest()->getParam('firsttime',0);

    //if(null==$this->aplid) $this->_forward('index','index');
    if ($this->getRequest()->isPost() && $firsttime==0) {
        if (!$form->isValid($_POST)) {
            // Failed validation; redisplay form
            $this->view->form = $form;
            return false;
        }
        var_dump($_SESSION);
        $values = $form->getValues();
        $db=new Util_Database();

        if($db->insertOrganization($values,$this->aplid))
            $this->_forward('final');
        else echo "An error occured while attempting to submit data. Please try agian";

    }


    $this->view->form=$form;
}

ここで何が問題なのですか?session_idをフォームに保存してから の前session_start()に設定しようとしましたが、まったく新しいセッションが開始されます。助けてください!

4

1 に答える 1

1

ステップ2で何か他のことが起こっているかどうかわからないので、これが役立つかどうかはわかりません。しかし、ここで説明します。
セッションデータを誤って上書きしている可能性があります。これが私が思いついたもので、いくつかのアイデアを与えるのに役立つかもしれません。

public function organizationAction() {

        $this->view->vals = "";
        $form = $this->getOrganizationForm();
        $db = new Util_Database();
        //This will only submit the form if the is post and firsttime == 0
        if ($this->getRequest()->isPost() && $this->getRequest()->getPost('firsttime') == 0) {
            //if form is valid set session and save to db
            if ($form->isValid($this->getRequest()->getPost())) {
                //We only want to initialize the session this time, if we do it
                //on the next pass we may overwrite the information.
                //initialize session namespace
                $session = new Zend_Session_Namespace('application');
                //get values from form, validated and filtered
                $values = $form->getValues();
                //assign form value appid to session namespace
                $session->appid = $form->getValue('appid');
                //assign session variable appid to property aplid
                $this->aplid = $session->appid;
                if ($db->insertOrganization($values, $this->aplid))
                    $this->_forward('final');
                else
                    echo "An error occured while attempting to submit data. Please try agian";
            } else {
                //if form is not vaild populate form for resubmission
                //validation errors will display of form page
                $form->populate($this->getRequest()->getPost());
            }
        }
        //if not post display form
        $this->view->form = $form;
    }

PSあなたがZFに行くつもりなら...ZFに行きなさい!:)

于 2012-04-30T04:43:12.227 に答える