cake(?) php 指定传递给控制器​​的表单数据

cake(?) php specify form data pass to controller

我是一个完全的 cakepHp 新手,但了解一些 php。

我有一个用于让会员续订的表格。他们唯一需要说明的是,他们是否要更改续签的期限(1 年、2 年...)

其余数据应从他们在 phpMyAdmin 数据库中的帐户 USER table 中获取。感兴趣的字段是:first_name、last_name、address1、address2、city、province、country、postal_code、email。这些应该以不可见的方式传递给表单(即用户看不到它们,但它们会进入发票和发票 table)。

此外,还有 2 个字段将被写入发票 table:备注(前 "user-renewal")和 is_renewal(应该 =“1 ").

到目前为止我有:

<form action="/member-renewals" id="UserDisplayForm" method="post" accept-charset="utf-8">
    <div style="display:none;"><input type="hidden" name="_method" value="POST"/></div>                         
    <ul class="formItemList"> 
    <li>
        <label>User Type</label>
        <select name="data[User][user_type_id]" id="UserUserTypeId">
            <option value=""></option>
            <option value="3">1-yr-Student (.00)</option>
            <option value="16">2-yr-Student (.00)</option>
        </select>
    </li> 

但我不知道如何查询和设置其他变量。此时用户已经登录,但我试过:

<first_name="data[User][first_name]" id="UserFirstName">

无果...

有什么想法吗?此处的搜索将我指向 ajax,这远远超出了我的范围。


编辑:我不确定这是否能回答您的问题。我在控制器目录中看不到任何相关内容。登录方式(来自login.ctp):

<?php
    $this->set('title_for_layout', 'User Login');
    $this->viewVars['keywords'] = 'User Login';
    $this->viewVars['description'] = $this->Text->truncate(strip_tags('This is where users can login'), 150,array('ending'=>'...', 'html'=>false,'exact'=>false));  
    $this->layout = "default_full_no_h1";
?>
<script src="/js/flowplayer-3.2.6.min.js"></script>
<script src="/js/flowplayer.embed-3.0.3.min.js"></script>

<?php echo $this->requestAction('/liveEditRegions/getRegion/37'); ?>   
<div class="greenForm">
<h2>Users Login</h2>
<?php echo $this->Session->flash(); ?>
    <?php echo $this->Form->create('User', array('url' => '/' . $this->request->url));?>
    <ul class="formItemList" style="width:350px;padding-bottom:40px !important;"> 

        <li><?php echo $this->Form->input('email', array('div'=>false, 'label'=>'Your Email Address'));?></li>
        <li class="clear"></li>
        <li><?php echo $this->Form->input('password', array('div'=>false, 'label'=>'Password'));?></li>  
        <li class="clear"></li>        
        <li style="width:125px;"><button type="submit" class="submitBTN" style="float:left;">Login</button></li>          
        <li style="width:150px;text-align:center;margin-right:0px;margin-left:10px;"><?php echo $this->Html->link('Forgot Your Password','/users/forgotPassword',array('style'=>'line-height:60px;'));?>  </li>
        <li class="clear"></li>                
   </ul>         
            <div class='clear'></div>
<?php echo $this->Form->end();?>

编辑:(controllers/UsersController.php):

function login() {
    $this->forceHTTPS();
    if ($this->userAuthenticate(false, true)) {
        $this->redirect('/users/index/');
    }
    $this->pageTitle = 'Login';
    if ($this->request->data) {
        if ($user_id = $this->User->validateLogin($this->request->data)) {

            $log = $this->addUserLog($user_id, 'Login');

            $params = array(
                'conditions' => array(
                    'User.id' => $user_id
                ),
                'contain' => array(
                    'UserType'
                )
            );

            $this->User->Behaviors->attach('Containable');
            $user = $this->User->find('first', $params);

            $this->Session->write('User', $user['User']);
            $this->Session->write('UserType', $user['UserType']);

            $this->Session->setFlash('Logged in successfully', 'flash_success');
            /*
              if(!$redirect = $this->Session->read('userRedirected')){
              $this->Session->delete('userRedirected');
              $redirect = '/users/index';
              $this->Session->delete('livetoolsRedirected');
              }
             */
            //$this->redirect($redirect); 
            $this->redirect('/users/index/');
        } else {
            $this->Session->setFlash('Unable to authenticate - please contact the CSPT administrator', 'flash_error');
            $this->request->data['User']['password'] = '';
        }
    }
}

编辑:我想这就是您要找的(来自:userscontroller.php)。这是旧的更新程序。新程序的用户将select 的类型更新。之后我会 post 这个。:

function cronRenew() {
    /*
      December - �Dues are due�:
      email is sent to all active members, who have not yet paid, indicating dues are due
      invoice is automatically generated for the members� dues
     */

    //Initiate the email component
   /* $email = new CakeEmail();

    $params = array(
        //'fields' => array('User.id', 'User.first_name', 'User.last_name', 'User.address1', 'User.created', 'User.city', 'User.province', 'User.country', 'User.postal_code', 'User.email'),
        'conditions' => array(
            'User.status' => 'active',
            'User.enabled' => '1',
        ),
        'order' => array(
            'User.id'
        ),
        'contain' => array(
            'UserType'
        ),
        'recursive' => 1
    );

    $this->User->Behaviors->attach('Containable');
    $users = $this->User->find('all', $params);

    //now that we have all active users, lets generate them invoices for renewal of memberships
    $errorEmailList = array();
    foreach ($users as $user) {

        if (isset($user['UserType']) && $user['UserType']['annual_dues_amount'] > 0 && !$this->User->hasUserPaidForTheComingYear($user['User']['id'])) {

            //Create the Invoice
            $invoice = array(
                'Invoice' => array(
                    'user_id' => $user['User']['id'],
                    'first_name' => $user['User']['first_name'],
                    'last_name' => $user['User']['last_name'],
                    'address1' => $user['User']['address1'],
                    'city' => $user['User']['city'],
                    'province' => $user['User']['province'],
                    'country' => $user['User']['country'],
                    'postal_code' => $user['User']['postal_code'],
                    'email' => $user['User']['email'],
                    'notes' => "User Membership Renewal",
                    'is_renewal' => '1',
                    'InvoiceItem' => array(
                        '0' => array(
                            'name' => 'User Membership Renewal',
                            'description' => 'Renewal for ' . $user['User']['first_name'] . ' ' . $user['User']['last_name'],
                            'price' => $user['UserType']['annual_dues_amount'],
                            'quantity' => '1',
                            //'hst_rate'=>$this->User->taxRate
                            'hst_rate' => 0
                        )
                    )
                )
            );

编辑:新的更新方法(来自:usercontroller.php):

# Send email of the Overdue @02/March/2015  
function cronInvoiceOverdueNew() {

    //Initiate the email component
    $email = new CakeEmail();

    $month = date('m');
    if($month < 3 && $month > 5){
        exit;
    }

    $params = array(
        'conditions' => array(
            'User.status' => 'active',
            'User.enabled' => '1',
        ),
        'order' => array(
            'User.id'
        ),
        'contain' => array(
            'UserType'
        ),
        'recursive' => 1
    );

    $this->User->Behaviors->attach('Containable');
    $users = $this->User->find('all', $params);

    $errorEmailList = array();
    foreach ($users as $user) {
        $inGoodStanding = $this->User->isUserInGoodStanding($user['User']['id']);
        $userPaidLastYear = $this->User->hasUserPaidForThisYearInTheLastYear($user['User']['id']);
        if ($inGoodStanding == false && !$userPaidLastYear) {
            try {
                //$month = '5';
                if ($month == '3') {
                    /* $message = "Attention " . $user['User']['first_name'] . " " . $user['User']['last_name'] . "\n\n" . "This is a notification to let you know you have outstanding invoice(s) that must be paid within a month or they will be charged a  late fee.\n\n" . 
                      "Thanks\n" .
                      */
                    /*$message = "You have an outstanding membership renewal invoice. Please follow the following link to pay your annual membership dues. A .00 late fee will be charged to invoices not paid by the end of February.\n\n" .
                            "Username: " . $user['User']['email'] . "\n" .
                            "Thank you for your continuing support \n" .
                      $message = "Thank you for your patience while undergoing site wide upgrades. Please note there will be no penalty for late renewals thus far. Please login and renew your account by visiting the 'Renewals' link at the bottom of the 'Memberships' dropdown. Please select your correct membership type and submit. An invoice will be created which you can select by clicking 'view' and pay by following through. A late fee of  will apply commencing May 2nd, 2015.";       

                    $email->subject('Avoid late fee for annual dues @ ' . Configure::read('Site.name'));
                } elseif ($month == '4') {
                    /* $message =  "Attention " . $user['User']['first_name'] . " " . $user['User']['last_name'] . "\n\n" . 
                      "This is a notification to let you know you have outstanding invoice(s) that if not payed by months end will have a  late fee added.\n\n" .
                      "Thanks\n" .
                     */
                    /*$message = "You have an outstanding membership renewal invoice. Please follow the following link to pay your annual membership dues. A .00 late fee will be charged to invoices not paid by the end of February.\n\n" .
                            "Username: " . $user['User']['email'] . "\n" .
                            "Thank you for your continuing support \n" .                               
                      $message = "Please log in and renew your account by visiting the \"Renewals\" link at the bottom of the \"Memberships\" dropdown. Please select your correct membership type and submit. An invoice will be created which you can select by clicking 'view' and pay by following through. A late fee of  will apply after 30 days, commencing May 2nd, 2015.";      

                    $email->subject('Last reminder for dues renewal @ ' . Configure::read('Site.name'));
                } elseif ($month == '5') {
                    /*$message = "Attention " . $user['User']['first_name'] . " " . $user['User']['last_name'] . "\n\n" . "This is a notification to let you know you have outstanding invoice(s).\n\n" .
                            "Thanks\n" .
                    $message = "Urgent: your account must be renewed by today to avoid late fees. Please log in and renew your account by visiting the \"Renewals\" link at the bottom of the \"Memberships\" dropdown. Please select your correct membership type and submit. An invoice will be created which you can select by clicking 'view' and pay by following through. A late fee of  will apply commencing tomorrow, May 2nd, 2015.";        

                    $email->subject('Outstanding Invoices @ ' . Configure::read('Site.name'));
                }


                $message .= "\n\n Renewing your membership allows you to be nominated for the Society awards, stand for election to various committees and, when appropriate, ensures your student's access to presentation and travel awards. The funds collected from membership dues and donations support the educational activities of the Society including the Trainee Travel Bursaries.";

                $email->lineLength = 200;
                $email->from(Configure::read('Site.contact.email'));
                $email->sender(Configure::read('Site.contact.noreply'));
                $email->to($user['User']['email']);
                $email->bcc('admin@xxxxxx.org');
                $email->send($message);
                //echo "<br/>".$user['User']['email']."<br/><br/>";
                echo nl2br($message);
                //echo "<hr/><br/><br/>";
            } catch (Exception $e) {
                $errorEmailList[] = $user['User']['first_name'] . ' ' . $user['User']['last_name'] . ' - ' . $user['User']['email'];
            }
            //die();                
        }
    }
    die('Reminder Email Cron Completed');
}

查看您的登录代码,您似乎将用户存储在您的会话中。所以你可以在你的控制器中访问它,并将它发送到你的视图,就像这样:

$user = $this->Session->read('User');
$this->set('user', $user);

这将创建变量 $user,您现在可以在视图中使用它来访问用户对象的属性,例如:

<?php echo $user['first_name']; ?>

我确实觉得你对 CakePHP 不是很熟悉,也许你应该尝试阅读基础知识:http://book.cakephp.org/2.0/en/tutorials-and-examples.html