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

时间:2015-03-10 00:27:45

标签: database forms cakephp

我是一个完整的cakepHp新手,但了解一些PHP。

我有一张表格,我正在用它来让会员续约。他们唯一需要指出的是,他们是否正在改变他们续约的期限(1年,2年......)

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

此外,还有2个字段将写入发票表:注释(例如“用户续订”)和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 ($40.00)</option>
            <option value="16">2-yr-Student ($72.00)</option>
        </select>
    </li> 

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

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

无济于事......

有什么想法?这里的搜索指向我,这远远超出了我。


编辑:我不确定这是否能解答您的问题。我在控制器目录中看不到任何相关内容。登录方法(来自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)。这是OLD更新程序。用户将选择续订类型的新程序。我将在此之后发布。:

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 $10 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 $10.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 $10 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 $10 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 $10.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 $10 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 $10 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');
}

1 个答案:

答案 0 :(得分:1)

查看您的登录代码,您似乎将用户存储在会话中。因此,您可以在控制器中访问它,并将其发送到您的视图,如下所示:

$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