【问题标题】:cakephp pre-fill email form with addresscakephp 用地址预先填写电子邮件表格
【发布时间】:2012-09-13 16:09:33
【问题描述】:

我在页面上有一个电子邮件地址列表,我想添加该功能,以便在单击电子邮件地址时打开一个电子邮件表单,该电子邮件地址已预先填写在收件人字段中。我该怎么做?

这是我已有的表格;

<?php echo $this->Form->create('Email', array('action'=>'email_send.php'));
    echo $this->Form->input('email',array('label'=>'To: ')); //i want the email address i clicked on to be automatically placed here.
    echo $this->Form->input('message',array('type'=>'textarea','label'=>'Message: '));
echo $this->Form->end('Send'); ?>

另外,如果有人对我将如何构建 email_send.php 文件以及传递变量和执行验证的最佳方式有任何提示,我也可以使用它。

【问题讨论】:

  • 电子邮件是来自数据库还是硬编码?

标签: forms email cakephp


【解决方案1】:

在蛋糕中,你可以像这样实现它.. 显然可以进行改进,但这是一个例子:

EmailsController.php(控制器)

function list_emails() {
    $this->set('emails', $this->EmailModel->find('all', array('fields' => 
                                                         array('id', 'email'))));

}

list_emails.ctp(查看)

echo '<ul>';
foreach($emails as $email) {
   echo '<li>' . $this->Html->link('Email: ' . $email['EmailModel']['email'],
          array('action'=>'process', $email['EmailModel']['id'])) . '</li>'; ?>
}
// generates a list of emails in the format:
// <a href="emails/process/1">Email: foo@foo.com</a>
echo '</ul>';

EmailsController.php(控制器)

function process($email_id = null) {
    if(!$email_id) { 
        $this->redirect(array('action'=>'show_emails')); // no id specified 
    }
    // check if the form has been submit, otherwise, get the info for the view..

    $this->EmailModel->id = $email_id;
    $email = $this->EmailModel->read();
    $this->set('email', $email);   
}

您现在可以在process 视图中获得该信息。

// echo $this->Form->input('email',array('label'=>'To: ','value'=>$email['EmailModel']['email']));

但这确实是很多工作。并且不要忘记值可以随时更改;让这有点毫无意义

【讨论】:

  • 太棒了。非常感谢您的详细解释!
【解决方案2】:

试试这个代码:

在控制器中

$this->set('email',$email); //$email is the mail address from database

在视图中

echo $this->Form->input('email',array('label'=>'To: ','value'=>$email));

您可以将电子邮件放入 '$this->request->data' 数组中,它会自动填充到视图中,如下所示: 你应该在你的控制器中添加这个

$this->request->data['Email']['email'] = 'YOUR_EMAIL_FROM_DATABASE';

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多