0

私の CakePHP アプリケーションには 2 つの連絡先フォームがあります。1 つは独自のコントローラー、モデル、およびビューを持ち、もう 1 つは要素内にあり、サイトのすべてのページのフッターから「クイック」連絡先フォームとしてアクセスできます。

両方のフォームのコードは同じです。この要素は、他のフォームが使用するコントローラーとモデルにアクセスするためのものです。ただし、要素はデータを送信したり電子メールを送信したりしていませんが、通常のページは問題なく機能します。

ISが機能している通常のフォームのMVCコードは次のとおりです。

<!-- Model: Model/Contact.php -->

<?php
class Contact extends AppModel {
    var $name = 'Contacts';
    public $useTable = false;  // Not using the database, of course.

    var $validate = array(
        'name' => array(
            'rule' => '/.+/',
            'allowEmpty' => false,
            'required' => true,
        ),
        'email' => array(
            'allowEmpty' => false,
            'required' => true,
        )
    );

    function schema() {
        return array (
            'name' => array('type' => 'string', 'length' => 60, 'class' => 'contact input'),
            'email' => array('type' => 'string', 'length' => 60, 'class' => 'contact input'),
            'message' => array('type' => 'text', 'length' => 2000, 'class' => 'contact input'),
        );
    }

}
?>

<!-- Controller: Controller/ContactsController.php -->

class ContactsController extends AppController
{
    var $name = 'Contacts';
    /* var $uses = 'Contact'; */
    var $helpers = array('Html', 'Form', 'Js');
    var $components = array('Email', 'Session');

    public function index() {
        if(isset($this->data['Contact'])) {
            $userEmail = $this->data['Contact']['email'];
            $userMessage = $this->data['Contact']['message'];

            $email = new CakeEmail();
            $email->from(array($userEmail));
            $email->to('email@example.com');
            $email->subject('Website Contact Form Submission');
            $email->send($userMessage);

            if ($email->send($userMessage)) {
                $this->Session->setFlash('Thank you for contacting us');
            } 
            else {
                $this->Session->setFlash('Mail Not Sent');
            }

        }
    }

    public function contact() {
        if(isset($this->data['Contact'])) {
            $userEmail = $this->data['Contact']['email'];
            $userMessage = $this->data['Contact']['message'];

            $email = new CakeEmail();
            $email->from(array($userEmail));
            $email->to('email@example.com');
            $email->subject('Website Contact Form Submission');
            $email->send($userMessage);

            if ($email->send($userMessage)) {
                $this->Session->setFlash('Thank you for contacting us');
            //  $this->redirect(array('controller' => 'pages', 'action' => 'index'));
            } 
            else {
                $this->Session->setFlash('Mail Not Sent');
            }

        }
    }

}
?>

<!-- View: Views/Contacts/index.ctp -->

<? 
$main = 'contact';
$title = 'quick contact';
?>
<div style="border-bottom: solid 1px #ccc;">
    <h1 style="position:relative; float:left;"><?php echo $main; ?></h1>
    <h2 style="position:relative;float:left;margin-top:15px; color: #869c38">&nbsp; &bull;&nbsp; <?php echo $title;?></h2>
    <br><br>&nbsp; &nbsp; 
</div>
<div class="clear"><br></div>
<div id="interior-page">
    <?php

    echo $this->Form->create('Contact');
    echo $this->Form->input('name', array('default' => 'name (required)', 'onfocus' => 'clearDefault(this)'));
    echo $this->Form->input('email', array('default' => 'email (required)', 'onfocus' => 'clearDefault(this)'));
    echo $this->Form->input('message', array('default' => 'message', 'onfocus' => 'clearDefault(this)'));
    echo $this->Form->submit();
    echo $this->Form->end();
    ?>
</div>

デフォルト レイアウトのフッターに表示される要素にある、動作していないクイック コンタクト フォームのビューを次に示します。

<?php

echo $this->Form->create('Contact');
echo $this->Form->input('name', array('default' => 'name (required)', 'onfocus' => 'clearDefault(this)'));
echo $this->Form->input('email', array('default' => 'email (required)', 'onfocus' => 'clearDefault(this)'));
echo $this->Form->input('message', array('default' => 'message', 'onfocus' => 'clearDefault(this)'));
echo $this->Form->submit();
echo $this->Form->end();

?>

フォームアクションを変更するさまざまな方法を試しましたが、わかりませんでした。

4

3 に答える 3

1

通常、cakeは「自動的に」フォームのアクションを作成します。たとえば、ビューViews / Contacts / index.ctpから呼び出された場合、アクションは/ contacts/indexに設定されます。要素の場合、Cakeはあなたが何をしようとしているのかを実際に推測できないため、アクションを手動で設定する必要があります。

$this->Form->create('Contact', array('action' => 'index'));

または、完全なURLを代わりに設定します。

$this->Form->create('Contact', array('url' => '/contacts/index'));
于 2012-04-26T20:47:12.627 に答える
1

そのフォームを作成する必要があるすべてのページで使用する連絡先モデルが含まれていることを確認してください。あなたの場合、それはあなたのレイアウトにあるので、それはおそらくあなたがそれをあなたのAppControllerに置くべきであることを意味します、それですべてのページがそれにアクセスできます。

また、フォームの送信先を指定する必要があります。

echo $this->Form->create('Contact', array(
        'url' => array('controller'=>'contacts', 'action'=>'contact')
    )
);

オフノート-最後の2行を組み合わせることができます。

echo $this->Form->end('Submit');

これにより、「送信」というテキストの送信ボタンが作成され、フォームも閉じます。

于 2012-04-26T20:48:00.087 に答える
1

これをありがとう!とても助かりました。

簡単なことですが、メールを 2 回送信しています。

ここに来たら:

$email->send($userMessage);

そしてここでも:

if ($email->send($userMessage))

最初のインスタンス ($email->send($userMessage)) は必要ありません。

乾杯

于 2013-02-20T19:17:13.587 に答える